Wednesday, May 29, 2013

Inheritance in java and Use of "super" and "super()"



Inheritance:

o   Inheritance is a process of reusing the features (Data members and member methods) of base class into derived class.
o   A base class is one which always gives its features to the derived class.
o   A derived class is one which contains its own features as well as the features from its base class.
o   Syntax for inheriting the features from the base class to derived class:
class <DerivedClassName> extends <BaseClassName>
{
      Variable declaration
      Method definitions
}
o   Here <DerivedClassName> and <BaseClassName> are the names of derived and base classes respectively.
o   Here “extends” is a reserved keyword which is used for inheriting the base class data members and methods into derived class and it also improves the functionality of the derived class.
o   It is strongly recommended to create an object of bottom most derived class since it contains the features of its own as well as its parent class features too.
o   When we create an object of bottom most derived class, JVM always creates the memory space for the data members of the super class first and later it creates the memory space for the data members of the derived class.
o   Once the base class is declared as final, it can’t be inherited into derived class.
o   Private data members of the base class will not be inherited into derived class.
o   For each and every class in Java, there is a super class called Object class. This class supplies gc () i.e. garbage Collector.
o   When the base class is containing final methods then in derived class the final methods will be inherited but we can’t change the definitions i.e. we can reuse but can’t override.

The “super” keyword:

o   “super” is a keyword which is used to differentiate the Base-class features from the derived-class features with in a derived-class if and only if they are similar.
o   The keyword “super” plays important roles at 3 levels. They are:
    • At variable level
    • At method level
    • At constructor level
o             “super” at variable level:
§  Whenever we inherit the data members from the base class to the derived class then there is a possibility  that the base class members are similar to the derived class members.
§  In order to distinguish the base class data members with derived class data members, the base class data members must be preceded with “super” keyword in the context of derived class.
§  Syntax:
super.baseClassDataMember;

o   “super” at method level:
§  Whenever we inherit methods from the base class to the derived class, there is a possibility that the base class methods are similar to the derived class methods.
§  In order to differentiate the base class methods derived class methods, the base class methods must be preceded with “super” keyword in the context of derived class.
§  Syntax:
super.baseClassMethodName();

o   “super” at constructor level:
§  Whenever we develop any application using the concept of inheritance, it contains base class, derived class and intermediate base classes.
§  When we create an object of the bottom most derived class, it calls object creation of its immediate super class automatically. An object of the immediate super class is nothing but an object of its top most super class i.e. an object of the bottom most derived class is nothing but an object of its immediate super class.
§  Hence, the constructors are being called from bottom most derived class to top most base class and they are being executed from top most base class to bottom most derived class.
§  An object of the derived class can be created with respect to either default or parameterized constructor.
§  Take the following diagram to understand the concept of super at constructor level:





o   super() :

o   This constructor is used to call the default constructor of the base class from the default constructor or parameterized constructor of derived class in context of derived class.

o   super(…)

§  This constructor is used to call the parameterized constructor of the base class from the default or parameterized constructor of the derived class in the context of derived class.

Rule 1: Whenever we want to call the default constructor of the base class from the default constructor of the derived class, using super () is optional.

Rule 2: Whenever we want to call the parameterized constructor of the base class from the parameterized constructor of the derived class, using super (…) is mandatory.

Rule 3: Whenever we want to call the default constructor of the base class from the parameterized constructor of the derived class, using super () is optional.

Rule 4: Whenever we want to call the parameterized constructor of the base class from the default constructor of the derived class, using super (…) is mandatory.

Note: whenever we make a call for either super () or super (…), they must be the first statement in the derived class constructors.

Example Program:

class Base
{
                        int a,b;
                        Base()  
                        {
                                     a=2; b=3;
                                    System.out.println("I am in default constructor of Base class.");
                                    System.out.println(" values of a:" +a);
                                    System.out.println(" values of b:" +b);
                        }
                        Base(int x)
                        {
                                    this();
                                    a=b=x;
                                    System.out.println("I am in single parameterized constructor of Base.");
                                    System.out.println(" value of a:" +a);
                                    System.out.println(" value of b:" +b);
}
Base(int x,int y)
{
            this(600);
a=x;
b=y;
System.out.println("I am in double parameterized constructor of Base.");
System.out.println("value of a" +a);
System.out.println(" value of b" +b);
}
}
class Derived extends Base
{
                        int m,n;
Derived()
{
super(400, 500);
m=100;
n=200;
                                    System.out.println("I am in default constructor of Derived class.");
System.out.println(" value of a"+m);
System.out.println(" value of b"+n);
}
           
                        Derived(int x)
{
this();
m=n=x;
                                    System.out.println("I am in single parameterized constructor of Derived class.");
System.out.println(" value of a"+m);
System.out.println(" value of b"+n);
}

Derived(int x, int y)
{
this(300);
m=x;
n=y;
System.out.println("I am in double parameterized constructor of Derived class.");
                                    System.out.println(" value of a"+m);
                                    System.out.println(" value of b"+n);
}
}
class SuperDemo
{
public static void main(String args[])
{
Derived d = new Derived(100,200);
}
}

No comments:

Post a Comment