Wednesday, November 9, 2016

super keyword in java

super keyword In java is a reference variable that is used to refer super class object.when we create object to super class,we can access only the super class members, but we can not access subclass members. But if we create object for subclass then can access all members of both super class and sub class. You must remember that we always create an object to sub class in inheritance.  What if super class members and sub class members may have same names. In this case,only sub class members are accessible by default.

Super can be used in three places: 
variable level
method level
constructor level

 Super at variable level:

Let we discuss with program without using super keyword:

class Test
{
int i=10;
}
class Demo extends Test
{
int i=20;
void display()
{
System.out.println("i="+i);
}
}
class Supervariable
{
public static void main(String args[])
{
Demo d=new Demo();
d.display();
}

}

Output:




Notice that in the above program Test and Demo classes have common properties but we got output for subclass only. In this case,how to access the super class members from subclass. For this purpose, super keyword has been invented. super refers to super class members from a subclass.

Read also: This keyword in java

 Rewrite the preceding program by using super keyword:

class Test
{
int i=10;
}
class Demo extends Test
{
int i=20;
void display()
{
System.out.println("i="+super.i);
}
}
class Supervariable
{
public static void main(String args[])
{
Demo d=new Demo();
d.display();
}

}
output:




Now we will see Super at method level :

super can be used to refer to super class methods. It used when super class method name and sub class method name same.

super.method()

Let us understand with example super at method level:

class Test
{
void print()
{
System.out.println(" WELCOME");
}
}
class Demo extends Test
{
void print()
{
System.out.println("WELCOME TO JAVA");
}
void display()
{
print();//it invoke sub class print() method
super.print();//it invoke super class print() method
}
public static void main(String args[])
{
Demo d=new Demo();
d.display();
}

}

Output:






Now we will discuss super at constructor level. It is used to call the super class constructor. There are two types
super()
super(..)

We need not call the default constructor of the super class, it is by default available to sub class. So, to call the parameterized constructor we have to write like below

super(value);

Let us understand the concept with program super at constructor level:

class Test
{
Test()
{
System.out.println("this is default constructor");
}
}
class Demo extends Test
{
Demo()
{
System.out.println("this is parameterized constuctor");
}
}
class SuperConstructor
{
public static void main(String args[])
{
Demo d=new Demo();
}
}
Output:



In the above program we created object for subclass,first of all the super class default constructor is called and then only the sub class constructor is called.

Let us understand parameterized constructor with example:

class Test
{
int a;
Test(int a)
{
this.a=a;
}
}
class Demo extends Test
{
int a;
Demo(int i,int j)
{
super(i);
i=j;
}
void display()
{
System.out.println("sub class="+a);

System.out.println("sub class a="+super.a);
}
}
class SuperDemo
{
public static void main(String args[])

{
Demo d=new Demo(10,20);
d.display();
}
}
Output:







No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...