JAVA Inheritance

One of the very useful aspect of the OOP is that a class can be derived from another class to extend the capabilities of a base class. This mechnism of driving or extending a sub class from the super class is called inheritance. 

Super Class or Base Class or Main Class

The class from which the class is inherited is called Super Class or Base Class

Sub Class or Derived Class


A class which is derived or inherited from Super Class is called  Sub Class or Derived Class.

Concept of the Inheritance is very simple to implement in JAVA. General syntax is as given below


class A {

}

class B extends A {

}

Class B is sub class which is derived from the base class A.

For example

package inheritancedemo;
class animal {
    public String name;
    public int height;


public   animal()
 {
     name="Donal";
     height=10;
 }

}

class Dog extends animal {
    public String breed;
    public String color;
   
    public Dog(String Breed,String Color){
     breed=Breed;
     color=Color;   
    }
   
    public void barking()
    {
        System.out.print("Tuffy is Barking");
    }
   
    }

/**
 *
 * @author Abu Bakar
 */
public class InheritanceDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
       Dog tuffy = new Dog("Lion Killer","Black");
       System.out.println(tuffy.name);
       System.out.println(tuffy.height);
       System.out.println(tuffy.breed);
       System.out.println(tuffy.color);
      
    }
   


This is very super simple example which not very technical to perform something very special for you but it just demonstrate the concept of inheritance in as super simple way.
We have two class animal which is Base Class and we derived a  Dog class. 

Interesting thing to note is that we have just created the object of sub class but is also useing the two properties name and height of the Super class also.

        
          Dog tuffy = new Dog("Lion Killer","Black"); // Object of the Sub Class


        System.out.println(tuffy.name);    // Properties of the Base Class used with object of
        System.out.println(tuffy.height);
  // sub class





Conceptually, inheritance in Java is implemented using subclassing and the extends keyword, followed by the parent class.The subclass inherits all of the public and protected members of its parent class. Additionally, a subclass inherits the package private members of the parent class if both reside in the same package. Having said that, it is very important no matter what you are trying to design, to keep the minimal set of the methods which class exposes publicly or to its subclasses.

One of the limitation of inheritance in JAVA is that a Sub Class can be derived only and only one Base class. A sub class can have only one super class.

But JAVA can achieve this goal using interfaces or Compositon, Which will be discussed in the next page.

If we want to use the properties and method of only Base Class then we use super keyword