What do you mean by polymorphism and inheritance?

Polymorphism – means the ability of a single variable of a given type to be used to reference objects of different types, and automatically call the method that is specific to the type of object the variable references. In a nutshell, polymorphism is a bottom-up method call. The benefit of polymorphism is that it is very easy to add new classes of derived objects without breaking the calling code (i.e. getTotArea() in the sample code shown below) that uses the polymorphic classes or interfaces. When you send a message to an object even though you don’t know what specific type it is, and the right thing happens, that’s called polymorphism. The process used by objectoriented programming languages to implement polymorphism is called dynamic binding. Let us look at some sample code to demonstrate polymorphism: 

double dim = 5.0; //ie 5 meters radius or width 
List listShapes = new ArrayList(20);
Shape s = new Circle();
listShapes.add(s); //add circle
s = new Square();
listShapes.add(s); //add square
getTotArea(listShapes, dim); //returns 78.5+25.0=103.5

/* Later on, if you decide to add a half circle then define 
   a HalfCircle  class, which extends Circle and then provide 
   an area(). method but your called method getTotArea(...) 
   remains same.
s = new HalfCircle();
listShapes.add(s); //add HalfCircle
getTotArea(listShapes, dim); //returns 78.5+25.0+39.25=142.75

/** called method: method which adds up areas of various 
    shapes supplied to it. 
public double getTotArea(List listShapes, double dim) {
	Iterator it = listShapes.iterator();
	double totalArea = 0.0;
	//loop through different shapes     
	while (it.hasNext()) {
		Shape s = (Shape) it.next();
		//polymorphic method call   
		totalArea += s.area(dim);
	return totalArea;

For example : given a base class/interface Shape, polymorphism allows the programmer to define different area(double dim1) methods for any number of derived classes such as Circle, Square etc. No matter what shape an object is, applying the area method to it will return the right results. Later on HalfCicle can be added without breaking your called code i.e. method getTotalArea(...)

Depending on what the shape is, appropriate area(double dim) method gets called and calculated.
Circle →  area is 78.5sqm Square →   area is 25sqm HalfCircle →   area is 39.25 sqm


Implementation inheritance (aka class inheritance) : You can extend an applications’ functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes reusability but improper use of class inheritance can cause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only on the behaviour of the superclass, not on the actual implementation. For example in the above diagram the subclasses should only be concerned about the behaviour known as area() but not how it is implemented.

Interface inheritance (aka type inheritance) : This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.  

Which one to use ? Prefer interface inheritance to implementation inheritance because it promotes the design concept of coding to an interface and reduces coupling. Interface inheritance can achieve code reuse with the help of object composition. If you look at Gang of Four (GoF) design patterns, you can see that it favours interface inheritance to implementation inheritance. 


core java 12 Inheritance 12


Explore Tutu'rself