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.

No comments: