/** * Created by PhpStorm. * User: Rahul * Date: 12/10/2015 * Time: 2:50 AM */ OOP's Concept

OOP concept : Inheritance

Core Java


Also known as IS-A relationship

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can design the new class that can inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

If several component required some common functionality then it's not recomended to write that functionality seperated in every class, we should write these common functionality in the seperate class and we have to make that calss as parent

Inheritance Example - Basic

		class P
		public void m1() {}

		class C extends P
		public void m2() {}

		class Test
				public static void main (String [] args)
					//case 1
					P  p = new P();
					p.m1();   // compiles fine
					p.m2();  // Will get a compile time error as "Cannot find m2()"

					//case 2
					C c = new C();
					c.m1();   // compiles fine
					c.m2();  // compiles fine

					//case 3
					P  p = new C();
					p.m1();   // compiles fine
					p.m2();  // Will get a compile time error as "Cannot find m2()"

					//case 4
					C c = new P(); // Compiler error as "incompatible types"

Brief Observation from above Example

Entire java API is implemented based on inheritance. Every java class extends from Object class which has most common and basic methods required for all java classes. Hence we can say “Object ” class is root class of all java methods.
A point to remember on inheritance - A java class cannot extend more than one class at a time so it wont provide support for multiple inheritance in classes, but it can extend more than one interface at a time so we can say java provides support for multiple inheritance w.r.t. interfaces.

Another Example on Inheritance


In this example, we derive a subclass called Cylinder from the superclass Circle, which we have created in the previous chapter. It is important to note that we reuse the class Circle. Reusability is one of the most important properties of OOP. (Why reinvent the wheels?) The class Cylinder inherits all the member variables (radius and color) and methods (getRadius(), getArea(), among others) from its superclass Circle. It further defines a variable called height, two public methods - getHeight() and getVolume() and its own constructors, as shown:


		// Define Cylinder class, which is a subclass of Circle
		public class Cylinder extends Circle {
		   private double height;   // Private member variable

		   public Cylinder() {      // constructor 1
		      super();              // invoke superclass' constructor Circle()
		      height = 1.0;
		   public Cylinder(double radius, double height) {  // Constructor 2
    super(radius);        // invoke superclass' constructor Circle(radius)
    this.height = height;

		   public double getHeight() {
		      return height;
		   public void setHeight(double height) {
    this.height = height;
		   public double getVolume() {
		      return getArea()*height;   // Use Circle's getArea()

A Test Drive Program: TestCylinder.java

		// A test driver program for Cylinder class
		public class TestCylinder {
public static void main(String[] args) {
Cylinder cy1 = new Cylinder();         // Use constructor 1
System.out.println("Radius is " + cy1.getRadius()
+ " Height is " + cy1.getHeight()
+ " Color is " + cy1.getColor()
+ " Base area is " + cy1.getArea()
+ " Volume is " + cy1.getVolume());

Cylinder cy2 = new Cylinder(5.0, 2.0); // Use constructor 2
System.out.println("Radius is " + cy2.getRadius()
+ " Height is " + cy2.getHeight()
+ " Color is " + cy2.getColor()
+ " Base area is " + cy2.getArea()
+ " Volume is " + cy2.getVolume());

Keep the "Cylinder.java" and "TestCylinder.java" in the same directory as "Circle.class" (because we are reusing the class Circle). Compile and run the program

The expected output is as follows :-

Radius is 1.0 Height is 1.0 Color is red Base area is 3.141592653589793 Volume is 3.141592653589793
Radius is 5.0 Height is 2.0 Color is red Base area is 78.53981633974483 Volume is 157.07963267948966

In Java multiple inheritance is not permitted. It was excluded from the language as a design decision, primarily to avoid circular dependencies -
here is an Example

//As you have learned the following is not possible in Java:
public class Dog extends Animal, Canine{


//However the following is possible:

public class Canine extends Animal{


public class Dog extends Canine{


Explanation :- The difference in these two approaches is that in the second approach there is a clearly defined parent or super class, while in the first approach the super class is ambiguous.
Consider if both Animal and Canine had a method drink(). Under the first scenario which parent method would be called if we called Dog.drink()?
Under the second scenario, we know calling Dog.drink() would call the Canine classes drink method as long as Dog had not overridden it.