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);
}
}

Thursday, August 01, 2019

Super Keyword at Method Level in java

Super Keyword at Method Level


Whenever we inherit the Base class method into Derived class, there is a possibility that base class method are similar to derived class method and JVM gets ambiguity problem. 

In order to differentiate between the base class methods and derived class methods, in the context of derived class and base class methods must be preceded by super keyword.

Syntax:
Syntax-of-super-keyword-at-method-level.jpeg
If we do not write super keyword before the base class method name then base class method name can be hidden in the content of derived class. that method will be considered by JVM as derived class method called by itself infinitely and recursive rest and gets run time error called as "Stack Over Flow Error".

Program: 1


class BaseClass

{
void display()
{
System.out.println("Java4");
}
}
class DerivedClass extends BaseClass
{
void display()
{
super.display();
System.out.println("Teachus");
}
}
class SuperEx
{
public static void main(String[] args) 
{
DerivedClass d= new DerivedClass();
d.display();
}
}
Output


Super at Method Overridden Level


We know that method overriding = Method heading is same + Method Body is different.    

The process of  pre-defining the  original method of  base class into various derived class for performing different operations  is called "Method Overriding". To applying the method overriding concept, we  need to use the inheritance principle. 

Program: 2

class Circle
{
void draw()
{
System.out.println("Draw Circle--main method");
}
}
class Rectangle extends Circle
{
void draw()
{
super.draw();
System.out.println("Draw Rectangle--Overriden method");
}
}
class Square extends Rectangle
{
void draw()
{
super.draw();
System.out.println("Draw Square -- Overiding Method");
}
}
class Shapes
{
public static void main(String[] args) 
{
Square s=new Sqaure();
s.draw();
}
}
Output:
====================
Draw Circle -- main method.
Draw Rectangle -- overridden method
Draw Square --  Overridden method
====================

Tuesday, July 30, 2019

Super Keyword at Variable Level in Java

Super Keyword in java



Super is a keyword  or reference variable in java which is used  to refer the parent object.  Super is an implicit keyword which is created by JVM for each and every program during compile time to playing the 3 important role. They are:
  1. Super keyword at data members or variable level.
  2. Super keyword at Method level.
  3. Super keyword at Constructor Level.

Purpose Of Super Keyword


Whenever we inherit the base class features into derived class, There is a possibility that base class features are similar to derived class and JVM gets ambiguity problems.  In Order to differentiate between the base class features  and derived class features must be preceded by super keyword. 

Syntax
Syntax-of-Super-Keyword-in-java.jpeg


Super Keyword at Data Member/Variable Level.


Whenever the derived class inherit the base class data members, there is a possibility that base class data members are similar to the  derived class data members and JVM gets ambiguity.

In Order to differentiate between base class data members  and derived class data members, in the context of derived class and base class data members must be preceded by super keyword.

Syntax
Syntax-of-Super-keyword-at-Variable-level-in-java
If we do not write a super keyword before base class data members then that data members is consider by JVM like as derived class data members and base class data members will be hidden.

Program:1

class BaseClass
{
int a;
}
class DerivedClass extends BaseClass
{
int a,b;
void set(int x,int y)
{
super.a=x;
this.a=y;
}
void add()
{
b=super.a+this.a;
}
void display()
{
System.out.println("value of Baseclass :" + super.a);
System.out.println("value of Derivedclass :" + this.a);
System.out.println("Summ :"+b);
}
}
class BaseDerivedClassEx
{
public static void main(String[] args) 
{
DerivedClass d=new DerivedClass();
d.set(10,20);
d.add();
d.display();
}
}
Output
Super-At-Variable-level-program.jpeg

Program :2

class First
{
String name="Java4";
}
class SecondName extends First
{
String name="Teachus";
void display()
{
String merge=super.name+name;
System.out.println("Full Name: "+ merge);
}
}
class FullName
{
public static void main(String[] args) 
{
SecondName s=new SecondName();
s.display();
}

}
Output
Super-Keyword-at-Variable-level-in-java.jpeg