As I can imagine that you are now aware of the term inheritance in java.When base and derive both class have constructor it feels difficult to call base class constructor in derive class.In previous tutorial we have done it in longer way.But

now its easy with the keyword super.super is keyword in java that have different uses but first we will discuss about how it can used to initialise base class variable with it’s constructor..


Super keyword for calling base class constructor in java

As the name suggest,it is used to access base class constructor directly in derived class.Syntax is easy you can see below

super(arguments);

  • Here arguments inside super must be matched with base class constructor.Arguments can be objects,variables or even null also..

Also read:- Buy new laptop with this tips
The proper way to use super keyword and how it works it described below with the help of following program..

Code 

class box1{
private int w;
private int h;
private int d;


box1(box1 obj)  //constructor with object
{
w=obj.w;
h=obj.h;
d=obj.d;
}
box1(int w1,int h1,int d1) //parameterized constructor
{
w=w1;
h=h1;
d=d1;
}
box1() //default
{
w=10;
h=10;
d=10;
}
box1(int l)
{
w=h=d=l;
}
void volume()
{
System.out.println(“Volume of box : “+(w*h*d));
}
}


//inherit box1
class box2 extends box1{
int ww;
box2(int w1,int h1,int d1,int ww1)
{
super(w1,h1,d1);  //base class constructor called
ww=ww1;
}
box2(box2 obj)
{
super(obj);     //base class constructor called again
ww=obj.ww;
}
box2()
{
super();  //calling base-class constructor
ww=10;
}
box2(int l,int l1)
{
super(l);  //calls base
ww=l1;
}
}
//main class
class supclass{
public static void main(String args[]){
//defining objects
box2 obj1=new box2(1,2,3,4);
box2 obj2=new box2(6,7);
box2 obj3=new box2();
box2 obj4=new box2(obj1);


obj1.volume();
System.out.println(“WW :”+obj1.ww);

obj2.volume();
System.out.println(“WW :”+obj2.ww);

obj3.volume();
System.out.println(“WW :”+obj3.ww);
obj4.volume();
System.out.println(“WW :”+obj4.ww);
//completed
}
}

Output

Volume of box : 6
ww:4
Volume of box : 216
ww:7

Volume of box : 1000
ww:10
Volume of box : 6
ww:4

Logic part

Here you can see we have defined different type of constructor inside class box1.Box2 inherits box1 and it calls the base class constructor.The appropriate constructor will be called with matching arguments section…
So this is all about how super is used to call constructor..

Also read:- The basic guide about class object in java
Stay here.. yet tutorial is not over..The second use of super is below

Super keyword to access base class hidden member in java

Yeah.. it is used to access the base class variable.Even base class and derived class both have same named variable we can initialise both of them in one class with help of super keyword..

Syntax is given below

super.member;

You will get much better idea when you will execute below short example

code

class a{
int k;   //same name but base class
}
class b extends a{
int k;  //same name but derived class
b(int aa,int bb)
{
super.k=aa;  //initialise base class variable
k=bb;
}
void show()
{
System.out.println(” k in base class : “+super.k);  //prints base class variable value
System.out.println(” k in derived class : “+k);
}
}
class samevariable{
public static void main(String args[])
{
b b1=new b(10,20); //object 
b1.show(); //calling function
}
}

output

k in base class : 10
k in derived class :20


logic part

Here we can see that both class have same named variable.But with help of super you can easily initialise base class variable in derived class.super.k=a is used to initialise the value of base class variable..
So it is about how super keyword is used to access member of base class..

I know that if you execute this two example you will get the actual concept of super keyword in inheritance in java..So don’t forget to share it with your friends and stay connected for more awesome conceptual tutorial of java.  

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *