Showing posts with label Object Oriented Principles. Show all posts
Showing posts with label Object Oriented Principles. Show all posts

Monday, March 16, 2009

Object Oriented Principles (Part 2)

Polymorphism:

Polymorphism is a Greek word that means “many forms”. Polymorphism is a phenomena in which same name is given to different implementations or same command name given to different commands.

“Same interface for multiple methods”

Let’s take an example of a dog, a dogs sense of smell is polymorphic. If the dog smells a cat, it will bark and run after it. If the dog smells its food, it will salivate and run to its bowl. The same sense of smell is at work on both situations. The difference is what is being smelled, that is, that type of data being operated upon the dogs nose.

We can assume that, polymorphism is a phenomena in which, a standard interface is given to more than one method or any implementation, and they will differ on the basis of the data being operated by them.

Many languages including Java, C++, PL/SQL, and Visual Basic implement polymorphism.

These languages implement polymorphism, from there different features, example method overloading is the technique by which many languages implement polymorphism. We will discuss method overloading but for now, Hossla, Hossla.

There is a confusion for method overloading and operator overloading that they are a part of Object Oriented technology, although that is not true because they are the features provided by any language to implement polymorphism.

Like Java implements polymorphism in three ways:

1) Polymorphism by Inheritance:

If any method accepts an object of a super class, the object of its sub class can also be accepted. The reason is that a subclass inherit all the properties and methods from the super class, the method which accepts the object of a super class may be using one of the properties or a method from that object, so those properties are also contained by the object of the sub class.

2) Overloading:

Implementing identically named methods in a same class that differ according to the arguments passed to them. It means that the same name will be given to different functions, and they will be called according to the type and number of arguments.

3) Interfacing:

Implementing identically named methods that take identical arguments in different classes.

C++ also implements polymorphism by operator overloading and the three defined above.

Inheritance:

Inheritance is one of the cornerstones of object oriented programming, because it allows creating class hierarchy. In terminology of Java the class which is inherited is called the

super class” and the class that does the inheriting is called “sub class”.

When a sub class is inherits a super class, it inherits the state and the behavior from all of its ancestors

opp1

In the above following example, the object of A has var1 and M1. The class A is inherited by class B, that why the object of class B contains the properties and methods of class A. The class is inherited by class C, so the object of class C contains the methods and properties of class B.

Object Oriented Principles (Part 1)

Object:

In the reality every thing is an object. Your pen, PC, cup, car etc are all objects.

Any thing that has physical existence is called an object. Even though you and me are also objects.

Every object has two distinct aspects:

1) Behavior

2) Characteristics

For an easy example, car is an object, it has methods to change gear, to accelerate, to break etc, these methods are simply called behavior of the car and it has properties like color, size, weight, model etc are all characteristics of the car.

Thus you saw that every object has properties which can be accessed by its methods.

In programming concepts every object has variables (properties) and functions (methods). Both of them are concealed in an object.

The basic idea behind the OOP languages was to combine both data and its member functions into a single unit called Object.

Object is the logical association between program variables and program methods.

Object contains variables that are only accessed by its member functions.

In other words an object is an instance (occurrence) of a class.

Class:

Continuing with the car example, the company which made the car has first designed the car model and its other aspects, they decided the number of gears, they decided how to change the gear, how to accelerate and other methods and properties of the car on just a piece of paper, there is no physical existence of the car yet,

They only designed the car architecture on the paper, which include car properties and car methods, and this architecture of the car is simply called its Class.

Class is a blue print of an object.

Class defines the architecture of an object, it defines the variables and the methods contained in an object.

Class does not have any physical existence, it just defines the object.

Encapsulation:

The word Encapsulation seems to have any biological meaning or it is derived from the medial dictionaries, a new hunter to Oops will think like that, but it is not true.

Data encapsulation and data hiding are the key terms in Object Oriented Programming.

The term Encapsulation means, “The data contained in a variable of an object is only accessed by its member functions”.

If a user wants to access certain data in the object, it will call its member function and that function will read the data and will return the value.

Thus we can assume that the data and its member function is encapsulated in a single unit called object and the data is only accessed by the functions defined in its class.

The Following Diagrams can give you a better approach:

 

opp1

Encapsulation

opp2