Top Java Interview Questions On Inhertitance


1.What is inheritance in java?

  • Inheritance is one of oops principle in java.
  • Inheritance is a mechanism of getting properties of one object to other object.
  • The idea of inheritance is simple i.e. When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write them yourself.

  • 2.Why We Use Inheritance In java?

      1.For Code Reusability
      2.For method overriding.

    3.How To Implements Inheritance in java?

  • By using extends keyword we can implement inheritance in java.
  • 4.Syntax of Inheritance In java?


    public class subclass extends superclass{
    // code
    }

    5.Can subclass can inherit the super class constructor?

  • No,
  • A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members of the class , so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
  • 6.Can a subclass inherits Private Members in a Superclass?

  • No, A subclass does not inherit the private members of its parent class.
  • 7.How do you implement multiple inheritance in java?

  • Using interfaces we can achieve multiple inheritance the code will be like following.

  • Interface A{
    }
    Interface B{
    }
    Interface implements C implements A,B{

    }

    8.What is The output of Following program?


    package com.practice;

    public class Parent {
    public int x = 10;

    }

    package com.practice;

    public class Child extends Parent {
    public int x = 20;
    public static void main(String[] args){
    Parent p = new Child();
    System.out.println(p.x);
    Parent p1 = new Parent();
    System.out.println(p1.x);
    Child c = new Child();
    System.out.println(c.x);
    }

    }

    output:


    10
    10
    20

    9.What is the output of the below program?


    package com.practice;

    public class A {
    public void method1(){
    System.out.println("im super class methods");
    }

    }

    package com.practice;

    public class B extends A {
    public void method1(){
    System.out.println(" child method");
    }
    public void method2(){
    System.out.println("hello world ");
    }

    public static void main(String[] args) {
    A a = new A();
    a.method1();

    B b = new B();
    b.method1();
    b.method2();

    A a1 = new B();
    a1.method1();


    }

    }

    output:


    im super class methods
    child method
    hello world
    child method

    10.What is the output of the below program?


    package com.practice;

    public class C {
    public static void method1(){
    System.out.println("static method of super class");
    }



    }

    package com.practice;

    public class D extends C {
    public static void method1(){
    System.out.println("static method of child class ");
    }

    public static void main(String[] a){
    method1();
    }

    }

    output:


    static method of child class

    No comments:

    Post a Comment