Sunday, March 27, 2011

Object Oriented Concepts

Software programs have grown increasingly complex over time. Many of them are almost impossibly complicated.

Object oriented analysis and object oriented programming are attempts to create a way to handle this complexity in a more natural way.

Object oriented analysis begins by identifying the objects involved in the solution. These objects map to the real world situation. You may have a customer object, an inventory object, a sale object etc. Next you define how these objects relate to each other and how they interact.

When it gets to the level of writing the programs there are certan object oriented principles that objects should adhere to. These principles are a bit difficult to get a hang of immediately. If fact, you will find obscure philosphical discussions of their meanings and implications in discussions of object oriented programming.

In code, a class is the definition of the object.

Abstraction
A class should be general, that is it should describe all customers not a single customer. It is an abstraction of an object. There are other more subtle consequences of this idea, such as seperating out common elements into a classes, or creating interfaces which contain collections of commen methods.


Encapsulation
Encapsulation incorporates two main ideas: one is that a class should be as complete and selfcontained as possible. It should not depend on other classes for its meaning or behavior. Second is the idea that the internal workings of a class should be private and invisible to other classes. Objects should be like legos, blocks that hook together. You just need to know what the holes and pegs are to get them to stick together. (in the case of classes these pegs and holes are properties and method signatures)

Polymorphism
Polymorphism means that the same object can behave differently in different contexts. Practically speaking it constists of few techniques. One is Operator and method overloading. That means making seveal operators or methods with the same name, but giving them different sets of parameters. You can call the same method but it will behave differently depending on the parameters you send it. A second technique is method overriding. This happens in inheritance. A child can override a method from the parent, giving it new featuresand functionaliry. A third technique also relates to inheritance. when initializing a class you can always substitute a child for the parent, thus getting a different behavior

Inheritance
Inheritance has to to with extending objects. When an object inherits from another object, it gets all the public properties and methods of the parent for free. It just adds its own special features. Inheritance usually follows a Generalization/specialization pattern. The parent is more generalized, the child more specific. For instance you might have a parent class called account, that holds all the information common to all accounts. Savings, Checking, Credit card could be children of account. All represent more specific types of accounts.


Elements of classes

Classes can contain the following elements:

Fields--these are class level variables that describe the class. Things like customerName, salePrice, etc. They are usually kept private to conform with the encapsulation principle.

Properties--these publicly expose fields. They usually contain a get method which returns the field value and a set method which allows a user to change the values. In non .net languages you have to write your own get and set methods called accessors and mutators.

Constructors--these are special methods that initialize the class. You can override constructors so that a class can be initialized differently in different situations.

Methods--these are things classes can do, such as perform calculations, retrieve data, etc.

Events--these are things a class can respond to such as a click of a mouse or a connection opening.

No comments:

Post a Comment