Java4teachus

Sunday, August 18, 2019

super() and super(..) constructor in java

Super at Constructor Level


➧ We develop the inheritance application, it is always recommended to create an object of bottom most derived class because inheritance features of intermediate class.

➧ When we create of object of bottom most class, first we get the memory space for data members from Top Most Base class,  second we get the memory space for data members for Intermediate Base class, Last we get the memory space for data members Bottom Most Derived class.

➧ In whatever the order the memory space  is created, in the same order "values must be initialized" with respect to class name i.e we must initialized data members for Top Most Base class, Data members for Intermediate class, data members for Bottom Most Derived class, Other wise we get the compile time errors occur.

➧  In inheritance base applications constructors are calling from bottom to top and executing from top to bottom. This rule is established communication between the base class and derived class constructors.

⏩Let us consider the following diagram which makes to understand communication between base class constructor & derived class constructor.
Super Keyword at Constructor level in java.jpeg
Super  Keyword at Constructor level are classified into 2 type. They are:
  1. super( )
  2. super(...)
➧ super( ) : It is used for calling super class default constructor from the derived class constructor. 
➧ super(...) : It is used for calling super class parameter constructor from the derived class constructor.

Important Rule


When we use the super() and super(..)as a part of derived class constructor then super class must be always as a part of  executable statement in the derived class constructor otherwise we get compile time errors.

Following diagram possibility of using super() & super(..)
super()-and-super(..)-constructor-in-java.jpeg
Rule 1 & Rule 3,  whenever the derived class constructor want to call default constructor of base class,in the context of derived class constructors we write super(). It is optional to use because class contain single form of default constructor.

Rule 2 & Rule 4, Whenever the derived class constructor want to call parameterized constructor of base class in the context of derived class constructor we must write as super(..). It is mandatory to use because base class constructor contains multiple forms of parameterized constructor.

Program:


class TopMostBaseClass

{
TopMostBaseClass()
{
super( )    // Control goes to java.lang.Object( )
System.out.println("TopMostBaseClass   to   Derivedclass");
}
class IntermediateClass
{
IntermediateClass()
{
super(); // control goes to TopMoseBaseClass
System.out.println("IntermediateClass   to   Derived class");
}
class BottomMostDerivedClass
{
BottomMostDerivedClass()
{
super();
System.out.println("BottomMostDerivedClass  to  Derived class");
}
}
class SuperRule
{
public static void main(String args[])
{
BottomMostDerivedClass   b=new BottomMostDerivedClass();
}
}

Program:1 Baseclass to Baseclass


class BaseClass
{
String a;
BaseClass()
{
System.out.println("Base class to derived class");
a="Java4";
System.out.println("BaseClass:"+a);
}
}
class DerivedClass extends BaseClass
{
String b;
DerivedClass()
{
super();
System.out.println("Derived class to Derived class");
b="Teachus";
System.out.println("DerivedClass:"+b);
}
}
class SuperClass
{
public static void main(String args[])
{
DerivedClass d=new DerivedClass();
}
}

Program: 2

class BaseCLass
{
int a;
BaseClass(int a)
{
System.out.println("Baseclass to Derivedclass");
this.a=a;
System.out.println("value of a="+a);
}
}
class DerivedClass extends BaseClass
{
int b;
DerivedClass(int a,int b)
{
super(a);
System.out.println("Derivedclass to Derivedclass");
this.b=b;
System.out.println("value of b="+b);
}
}
class SuperClass1
{
public static void main(String[] args) 
{
DerivedClass d=new DerivedClass(10,20);
}
}

No comments:

Post a Comment

Thanks for visiting my site..