Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 12 October 2017

Interview Questions on Super Keyword in Java

Java Super Keyword Interview Questions

Interview Questions on Super Keyword in java

In this article, We are going to discuss the most important topic of core java interview is super keyword in java. Here we will focus on some important interview questions on super keyword in java which is frequently asked in java interviews.

Let's start basic super keyword java interview questions for 1 year experience and fresher both.


(1) What is super keyword in java?

Super keyword in java is a reference variable which is refers immediate parent or super class object. In other words, Super keyword is used to access the members of parent class.

Super keyword point to immediate parent class object. It works with object only of parent class.


(2) Write some points about super keyword in java?

There are some use of super keyword in java.
  • Super keyword can be used to refer parent class instance variables.
  • Super keyword can be used to invoke parent class method.
  • Super keyword can be used to invoke parent class constructor.

(3) Can we access parent class variables in child class by using super keyword?

Yes, We can access parent class variable in child class by using java super keyword.

For example:

class Parent
{
int a = 20;
}
class Child extends Parent
{
int a = 30;
void show()
{
System.out.println(a);//print child class value of a
System.out.println(super.a);//print parent class value of a
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}

Output: 30
             20

You can see in the above example, There are same data member in both parent and child class. If you will not use super keyword in child class, it will print only child class value by default i.e 30 because priority goes to child. If you want to print both child and parent object values you have to use super keyword in sub class.


(4) Can we call parent class method in sub class by using super keyword.

Yes, We can access parent class method in child class by using super keyword.

For example:

class Parent
{
void display()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void display()
{
System.out.println("child");
super.display();
}
public static void main(String args[])
{
Child c = new Child();
c.display();
}
}

Output: child
             parent


(5) Can we call parent class constructor in sub class constructor i.e constructor chaining by using super keyword?

Yes, We can call parent class constructor in sub class constructor by using super keyword but super keyword must be the first statement in sub class constructor.

Syntax:

class ParentDemo
{
ParentDemo()
{}
}
class Child extends ParentDemo
{
Child()
{
super();//must be first statement i.e in first line
//statement;
//statement;
}
psvm()
{}
}


(6) Can we use both "this" and "super" in constructor?

No, It is not possible in java we cannot use both this and super keyword together in java constructor because this and super should be the first statement in any java constructor.


(7) What is difference between this() and super()?

There are some difference between java this() constructor and super() constructor.

Java this()
  • It is used to access current class constructor.
  • It can be used inside another constructor of same class.
  • It can be used to remove ambiguity error when we have data members and local are same name.
For example: Remove ambiguity error

class Demo
{
int a;
int b;
Demo(int a, int b)
{
this.a = a;
this.b = b;
}
}
}

Java super()
  • It is used to access parent class constructor from sub class.
  • It can be used to only inside child class constructor.
  • It doesn't remove any ambiguity error from programs.

(8) Can we use super keyword in static method of a sub class for calling parent class method?

No, We cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.

Read More:

Interview Questions on Static Keyword in Java.
Interview Questions on Final Keyword in Java.
Interview Questions on Multithreading in Java.
Struts Framework Interview Questions.
Interview Questions on Java String
Some Basic Programs on Java Keywords.


Above all the super keyword interview questions and answers are quite useful for java interviews.

Share:

6 comments:

  1. This is best tutorial for java learner thanks

    ReplyDelete
  2. java is my favourite language...:)

    ReplyDelete
  3. Hi Anurag Singh,


    What a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.


    . Why am I allowed to overwrite the final field PRIZE in a subclass? I expected a compile error but got nothing.
    Java Code:
    class Final {
    public static final String PRIZE = "$64,000";
    }

    public class TestFinal extends Final {
    public static final String PRIZE = "2 cents";

    public static void main(String[] args) {
    System.out.println(TestFinal.PRIZE);
    }
    }

    I look forward to see your next updates.


    Regards,
    Morgan

    ReplyDelete
  4. Since, You have used PRIZE as static member which cannot be overridden
    In above example static member PRIZE is perform member hiding in subclass
    member hiding- creating new member.

    ReplyDelete
  5. 11
    12 13
    14 15 16
    17 18 19 20
    How to do it

    ReplyDelete
  6. Why we can not overwrite Statics methods

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate