Pages

super keyword in Java



-> Java language a keyword called ‘super’ which can be used for 2 purposes.They are:
1) To call the constructor of super class from sub-class constructor.
2) To call the members of super class from sub-class members.
->The following program illustrates the super keyword and multi-level Inheritance.
/* Program for Multi-level Inheritance and to demonstrate super keyword */
class Square
{
 int l;
 Square(int x)
 {
 l=x;
 }
 void area()
 {
 System.out.println("Area of Square = " +  l*l);
 }
}// End of class Square
class Rectangle extends Square
{
 int b;
 Rectangle(int x,int y)
 {
 super(x); /*passes x to constructor of  Square class */
 b=y;
 }
 void area()
 {
 super.area(); // calls area() of Square class
 System.out.println("Area of Rectangle = " +  l*b);
 }
} //End of class Rectangle
class Cube extends Rectangle
{
 int h;
 Cube(int x,int y, int z)
 {
 super(x,y);
 h=z;
 }
 void area()
 {
 super.area(); // calls area() of Rectangle class
 System.out.println("Volume of cube= " +  l*b*h);
 }
} //End of class Cube
class SuperDemo2
{
 public static void main(String args[])
 {
 Cube c1 = new Cube(10,20,30);
 c1.area();
 }
}
Note:
1) The statement 'super()' should be the first statement in the constructor of a sub class.
2) By default, every class constructor will have a statement called 'super()' as the first statement in its constructor. Because of this statement only, the super class objects are created before sub class objects are being created.

The following program demonstrates Hierarchical Inheritance
//Program fro Hierarchical Inheritance
class Square
{
 double l=10.0;
}
class Rectangle extends Square
{
 double b=20.0;
 void area()
 {
 System.out.println("Area of Rectangle= " + l*b);
 }
}
class Circle extends Square
{
 void area()
 {
 System.out.println("Area of circle= " + 3.14*l*l);
 }
}
class IDemo6
{
 public static void main(String args[])
 {
 Rectangle R=new Rectangle();
 R.area();
 Circle C=new Circle();
 C.area();
 }
}
NOTE: In Java, for the reference of a super class, we can assign the object of a sub-class. With that reference, we can call the methods defined in super class only. When a method is called with that reference, the JVM first checks whether that method is defined in super class or not. If defined, it moves to sub most object and execute that class method. If not available in the sub-class, it moves to next immediate super class and repeats the procedure until the method is found.
->The following program demonstrates super-class reference and sub-class concept.
/** Super class reference, subc-class object */
class A
{
 void fun1()
 {
 System.out.println("Inside fun1 of A");
 }
 void fun2()
 {
 System.out.println("Inside fun2 of A");
 }
 void fun3()
 {
 System.out.println("Inside fun3 of A");
 }
}//end of class A
class B extends A
{
 void fun2()
 {
 System.out.println("Inside fun2 of B");
 }
 void fun4()
 {
 System.out.println("Inside fun4 of B");
 }
}//end of class B
class C extends B
{
 void fun3()
 {
 System.out.println("Inside fun3 of C");
 }
 void fun5()
 {
 System.out.println("Inside fun5 of C");
 }
}
class Inheritance3
{
 public static void main(String args[])
 {
 A a1=new C();
 a1.fun1(); // prints fun1() of A
 a1.fun2(); // prints fun2() of B
 a1.fun3(); // prints fun3() of C
/* a1.fun4(); // cannot be called since it is  not defined in class A
 a1.fun5();  // cannot be called since it is  not defined in class A */
 }
}// end of class Inheritance3