Inheritance

What is Inheritance in java ?


Inheritance is capability of one class of things to derive capabilities from another class.For example, If Fruit is a class and Tomato inherits it, then tomato will have properties of Fruit class but vice versa is not true.

Suppose you work for a company, where managers are treated differently from other employees. They both are paid salary but managers get bonus for a particular that they do in a month  i.e. all managers are employees but all employees are not managers.

Now you need to define two classes

1. Employee

2. Manager

Manager class that inherits from the Employee class.

We use keywords - "extends" or "implements" for inheritance. If derived class inherits interface then we use "implements" keyword, if it inherits class then we use "extends" keywords

Superclass / base class/ parent class -> is the class from which other classes inherits its properties.

Subclass / derive class / child class -> is the class that inherit properties from the parent class.

class Employee

{ // Class Code }

 

class Manager extends Employee

{ // Class Code }

 

RULES OF INHERITANCE

  1. Sub Class cannot access the private fields and methods of Super Class.
  2. Sub Class cannot inherit more than one class at a time.
  3. Super Class can hold the reference to the Sub Class but can access only those fields and methods which are derived or inherited from it.
  4. While overidding the method of Super Class in the Sub Class we cannot decrease the scope of access modifiers (we will study over-riding in the polymorphism context.)


TYPES OF INHERITANCE

SINGLE INHERITANCE

When a Child class inherits from one parent class/interface.

MULTIPLE INHERITANCE

When a Child class inherits from two or more parent interfaces (Java does not support multiple class inheritance).

HIERARCHICAL INHERITANCE

When many Child classes inherit from single parent class/interface.

MULTILEVEL INHERITANCE

When a Child class inherits from a class/interface that itself inherits from the another class/interface.

HYBRID INHERITANCE

Combination of two or more forms of inheritance is called as Hybrid Inheritance.



PUBLIC INHERITANCE OR DERIVATION

  • All the inheritance in Java are public inheritance.There is no private or protected inheritance like C++ in Java language.
  • Public Inheritance means Sub Class can access the public and protected members of the Super Class but not the private members of the Super Class.
  • Public Derivation or Inheritance does not change the access specifiers for the derived or inherited members in the Sub Class.

this AND super KEYWORDS

  1. In java "this" keyword is a reference to the current object
  2. "this" is used to refer the current class instance variable or to call the current class method or to call the current class constructor.
  3. "this" keyword can also be used as an argument. We can pass it as an argument in the method call and constructor call.
  4. "super" keyword is used to call the Super Class method and its constructor.
  5. In the constructor, when we call constructor of another class then it has to be the first statement.
  6. Both this and super has to be the first statement in the constructor call that's why you can never use this and super together in the constructor call.
  7. this keyword will refer to the constructor of the same class and super keyword will refer to the constructor of Super Class.


POINTS TO REMEMBER

  • We know that Sub class cannot access the private fields of the Super class.We can initialize the private fields of the Super class by using the super keyword as the first statement in the constructor of Sub Class.
  • Compiler always call the default constructor of the Super Class by making super() keyword as the first statement which is not visible to us.
  • If Super class has parameterized constructor, then the programmer has to call it explicitly, it is not called by default.
  • If Super class has no default constructor and it has one parameterized constructor, somehow we forget to call the parameterized constructor explicitly from the Sub class,  then the Java compiler ( javac ) will report an error.


LET'S TAKE A LOOK AT PROGRAM

( read comments in the program for better understanding. )