Java Inheritance Interview Questions and Answers

51. What will be the output of the following program?

a)

class A 
{	
   int x;
   void m1() {
      System.out.println(x);	  
   }
}
public class B extends A 
{
   int y;
   void m1() {
      System.out.println(y);	 
   }
}
public class Test {
public static void main(String[] args) 
{
    B b = new B();
    b.x = 20;
    b.y = 30;
    b.m1();
  }
}

Ans: 30

b)

class A 
{	
   int x;
   void m1() {
      System.out.println(x);	  
   }
}
public class B extends A 
{
   int y;
   void m1() {
      super.x = y - 1;	 
      System.out.println(y+ " " +x);	 
   }
}
public class Test {
public static void main(String[] args) 
{
   B b = new B();
   b.x = 20;
   b.y = 30;
   b.m1();
  }
}

Ans: 30 29

c)

class A {	
   int x = 20;
   void m1(){
      System.out.println(x);	  
   }
}
public class B extends A {
   int y = 30;
   void m1(){
      super.x = super.x + 1;	 
      System.out.println(super.x+ " " +y);	 
   }
}
public class Test {
public static void main(String[] args) {
   A a = new B();
   a.m1();
  }
}

Ans: 21 30

d)

class A {	
   public int x;
   private int y;
}
public class B extends A {
   void m1(){
      super.y = super.x + 1;	 
      System.out.println(super.x+ " " +super.y);	 
   }
}
public class Test {
public static void main(String[] args) {
   B b = new B();
   b.x = 20;
   b.y = 30;
   b.m1();
  }
}

Ans: Compilation error.

e)

class A 
{	
  public int x;
  public int y;
  A() {
    x = 10;
    y = 20;
  }
}
public class B extends A {
  int z;	
  B() {
    super();
  }
}
public class Test {
public static void main(String[] args) 
{
   B b = new B();
   System.out.println(b.x+ " " +b.y);
  }
}

Ans: 10 20

f)

class A {	
   public int x;
   protected int y;
}
public class B extends A {
   int y;	
   void m1(){
      super.y = 10;
      System.out.println(x+ " " +super.y+ " " +y);
   }
}
public class Test {
public static void main(String[] args) {
   B b = new B();
   b.x = 20;
   b.y = 30;
   b.m1();
  }
}

Ans: 20 10 30

g)

class A {	
   public int x;
   protected int y;
}
public class B extends A {
   int y;	
   void m1(){
     super.y = super.x + y;
     System.out.println(x+ " " +super.y+ " " +y);
   }
}
public class Test {
public static void main(String[] args) {
   B b = new B();
   b.x = 20;
   b.y = 30;
   b.m1();
  }
}

Ans: 20 50 30


Recommended Interview Questions


Hope that these Java inheritance interview questions have covered enough questions with the best possible answers. I hope that you will have understood and solved these inheritance interview questions. New questions based on inheritance for interviews will be updated regularly for best practice. Please, share it on social networking sites for your friends.
All the best!!!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.