Super Keyword in Java

 


Super Keyword in Java -


"Super Keyword" in Java is used to access the member of super class.

We can use super Keyword in 3 different ways :

a) To call the super class variable.

b) To call the super class method.

c) To call the super class constructor.


a) To call the super class variable :

If the super class variable name and sub class variable name both are same then it is called "variable shadow". Here sub class variable will hide super class variable.

If we create an object for sub class then by default it will access sub class variable.


b) To call the super class method :

Whenever super class method name and sub class method name both are same and if we

create an object for sub class then by default it will access sub class method.

If we want to access super class method then we should use super keyword.


c) To call the super class constructor : (Constructor Chaining)

Whenever we write a class in java and we don't write any kind of constructor to the class then the java compiler will automatically add one default constructor to the class.

THE FIRST LINE OF ANY CONSTRUCTOR IS RESERVED EITHER FOR super() or this()

keyword.

In the first line of any constructor if we don't specify either super() or this() then the compiler will automatically add super() to the first line of constructor.

Now the purpose of this super() [added by java compiler], to call the default constructor or No-Argument constructor of the super class.

In order to call the constructor of super class as well as same class, we have total 4 cases.


case 1 :

super() :- Automatically added by compiler and it is used to access default or no argument constructor of super class.

For instance -

class Parent

{

public Parent()

{

System.out.println("Parent class constructor");

}

}

class Child extends Parent

{

public Child()

{

System.out.println("Child class constructor"); }

}

public class SuperConstructor

{

public static void main(String[] args)

{

Child c = new Child();

}

}


Case 2 :

super(String name) :- Used to call parameterized constructor of super class.

For instance -

class A

{

public A(String name)

{

System.out.println("My name is :"+name);

}

}

class B extends A

{

public B()

{

super("Scott");

System.out.println("No Argument constructor of B class");

}

}

public class SuperConstructor

{

public static void main(String[] args)

{

new B();

}

}


Case 3 : 

this() :- Used to call no argument constructor of current class.

For instance -

class A

{

public A()

{

System.out.println("No Argument constructor of A class");

}

public A(String name)

{

this();

System.out.println("My name is :"+name);

}

}

class B extends A

{

public B(String name)

{

super(name);

System.out.println("No Argument constructor of B class");

}

}

public class ThisConstructor

{

public static void main(String[] args)

{

new B("Scott");

}

}


case 4 :

this(int x) :- Calling the parameterized constructor of current class.

For instance -

class Base

{

public Base()

{

this(15);

System.out.println("Base class No Argumenet");

}

public Base(int x)

{

System.out.println("Base class Parametrized :"+x);

}

}

class Derived extends Base

{

}

public class ThisDemo

{

public static void main(String[] args)

{

new Derived();

}

}

Interview Questions on super Keyword

- What is the use of super keyword in Java?

- What is variable shadowing in Java?

- Can we use super and this in the same constructor?

- What is constructor chaining in Java?

- Does Java compiler always add super() automatically?

Conclusion 

If you found this post helpful, share it with your friends who are learning Java. Drop your questions or doubts in the comment section below — I will be happy to help






Comments

Popular posts from this blog

How To Hack Wifi | Hack Wifi Password | TECH WORTH MIND

Remove Tools From Termux | How can I reopen the installed tool in termux? | TECH WORTHY MIND