Saturday, 7 December 2013

Super Keyword in Java


Use of super keyword comes in java when inheritance is implemented i.e. when we have super class and sub classes as explained in the topic Inheritance in Java.
Usage are as explained below:
1.      To call a super class method :
Normal method call:
When we want to call a method of super class in a sub class, it can be done by using super.mathodname(). It is demonstrated in below example:

Parent and child class
reference of subclass calling sub class method
output


Overridden method call:
When the overriding method needs to call the overridden method of the parent class, it can be called using super.sameMethodName().
It is demonstrated in below example:
Consider the super class from above example.Subclass has overridden the super class method and in the overridden method, it has called the same method of super class using super.

sub class overriding super class method method()

output

2.      To explicitly call the super class constructor(both default and paramaterized) : Though it is not needed because the super class constructor is implicitly called from the sub class constructor.  
      
      Default constructor is called in below manner :


      Note : while calling constructor of base class from sub class constructor, the super() must be the first statement.

      Similarly paramarized constructor can be called.



3.      To access non private super class members : All non private members in parent class can be accessed using super keyword as shown in below example :


Inheritance in Java


 Inheritance in simple language means to have something in common in child with parent. Inheritance in java also has the same concept like a child inherit some characteristics and surname from parent. It is one of the basic OOP concepts used in java along with Encapsulation, Polymorphism and Abstraction.

            Inheritance is a way to define IS-A relationship between classes. Inheritance is used when we have to design a new class and this new class needs some code which another class already has. In implementing this, the fields and the methods can be reused without writing them again.

        In Java, inheritance is achieved through extends and impelements keywords. By using these keywords properties of one object can be acquired in another object. The class from which n new class is derived is known as subclass and the class from which subclass is derived is called superclass.

For example:
Consider the Animal as a base class and Cat as the derived class from it and follow the code written below:


Base Class : Animal

Child class : Cat

Main Class
The output for this program would be :

                                          Legs :4
                                          sounds as meow

Diagrammatically, inheritance is represented as below:

Inheritance is represented using an arrow
Types Of Inheritance:
There are three types of inheritance in java.
1. Single
2. Multilevel
3. Hierarchical

As shown in below diagram:

Types of Inheritance in Java