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

Wednesday 6 September 2017

Java Inheritance Programming Interview Questions

Programming Exercises on Inheritance in Java Interviews

Programming Exercises on Inheritance in Java Interviews

In last post, we have been learned Top 10 java programming interview questions and answers but here we will learn some java inheritance programming interview questions.

Now let's start java inheritance questions and answers for core java interviews.


(1) What is the output of following program?

class Parent
{
void show()
{
System.out.println("Parent Class");
}
}
class child extends Parent
{
void show()
{
System.out.println("Child Class");
}
public static void main(String args[])
{
Parent p = new Child();
p.show();
}
}

Output: Child Class


(2) What is the output of following program?

class First
{
int a = 30;
}
class Second extends First
{
int a = 70;
public static void main(String args[])
{
First s = new Second();
System.out.println(s.a);
}
}

Output: 30


(3) What is the output of following program?

class X
{
public X()
{
System.out.println("Constructor X");
}
}
class Y extends X
{
public Y()
{
System.out.println("Constructor Y");
}
}
class Z extends Y
{
public Z()
{
System.out.println("Constructor Z");
}
public static void main(String args[])
{
Z z =
new Z();
}
}

Output: Constructor X
              Constructor Y
              Constructor Z


(4) What is the output of following program?

class Parent
{
static void method()
{
System.out.println("Parent method");
}
}
class Child extends Parent
{
static void method()
{
System.out.println("Child method");
}
}
class Test
{
public static void main(String args[])
{
Parent p =
new Child();
p.method();
}
}

Output: Parent method


(5) What is the output of following program?

class A
{
void display1()
{
System.out.println("display1");
}
}
class B extends A
{
void display2()
{
System.out.println("display2");
}
}
class C extends A
{
void display3()
{
System.out.println("display3");
}
}
class Demo
{
public static void main(String args[])
{
C c = new C();
c.display3();
c.display2();
c.display1();
}
}

Output: compile time error


(6) What is the output of following program?

class First
{
    {
       System.out.println(10);
     }
}
class Second extends First
{
    {
       System.out.println(20);
     }
}
class Third extends Second
{
    {
       System.out.println(30);
     }

public static void main(String args[])
{
Third t = new Third();
}
}

Output: 10
              20
              30


(7) What is the output of following program?

class Parent
{
private void run()
{
System.out.println("run fast");
}
}
class Child extends Parent
{
public static void main(String args[])
{
Child c =
new Child();
c.run();
}
}

Output: Compile time error


(8) What is the output of following program?

class A
{
     
static
     {
          System.out.println("a");
     }
     {
          System.out.println("i");
     }
}
class B extends A
{
     static
     {
          System.out.println("e");
     }
     {
          System.out.println("o");
      }
public static void main(String args[])
{
B b = new B();
}
}

Output: a
             e
             i
            o


(9) What is the output of following program?

class A
{
int a = 30;
}
class B extends A
{
int a = 40;
void show()
{
System.out.println(super.a);
}
public static void main(String args[])
{
B b = new B();
b.show();
}
}

Output: 30


(10) What is the output of following program?

class A
{
String name = "God";
}
class B extends A
{
String name = "is";
 {
   System.out.println(super.name);
  }
}
class C extends B
{
String name = "great";
  {
     System.out.println(super.name);
    }

public static void main(String args[])
{
C c =
new C();
System.out.println(c.name);
}
}

Output: God
              is
              great

I hope, this article will clear your all conceptual problems on inheritance in java programming interview questions.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate