Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Top Java Inheritance Interview Questions (2022) | TechGeekNxt >>





Top Java Inheritance Interview Questions (2022)

  1. What is Java Inheritance?
  2. What are the different types of inheritance in Java?
  3. Why to use inheritance in java?
  4. How to access any data member or methods of the parent class in Java?
  5. Is it possible for a class to extend itself in Java inheritance?
  6. What is the output of the program, when derived class extends base class?
  7. Why is Multiple Inheritance not supported in Java?
  8. What would be the output of the program, if we print n from the main class?
  9. What would be the output of the program, if we make an object of derived class?

Q: What is Java Inheritance?
Ans:

Inheritance is a mechanism by which one class obtains a property from another. A child, for example, inherits the characteristics of his or her parents. We can reuse the existing class's fields and methods by using inheritance. As a result, inheritance promotes reusability and is an important concept in OOPs.

Syntax: The extends keyword extends a class and implies that it is inherited by another class.

class subClass extends superClass
{
   //methods and fields  
}  

Q: What are the different types of inheritance in Java?
Ans:

Below are the different types of inheritance available in Java.

  1. Single Inheritance
    One class extends another class in Single Inheritance (one class only). As per below diagram, Class B only extends Class A. Class A is the superclass, and Class B is the subclass.
  2. Multiple Inheritance
    Multiple inheritance is not supported in Java.Multiple Inheritance is a type of inheritance in Java that occurs when one class extends more than one class.
  3. Multilevel Inheritance
    One class can inherit from a derived class in Multilevel Inheritance. As a result, the derived class becomes the new class's base class. In below diagram, Class C is subclass of B and B is a of subclass Class A.
  4. Hierarchical Inheritance
    One class is inherited by many subclasses in Hierarchical Inheritance.
  5. Hybrid Inheritance
    Java doesn't support hybrid Inheritance. It is a combination of Single and Multiple inheritance.

Take a look at our Suggested Posts :

Q: Why to use inheritance in java?
Ans:

  1. For Method Overriding (to achieve runtime polymorphism): In Java, method overriding occurs when a subclass (child class) has the same method name as the parent class. Runtime polymorphism, also known as Dynamic Method Dispatch, is a process that resolves a call to an overridden method at runtime instead of compile-time.
  2. Code Reusability: The code defined in the parent class could be used by the child class without having to rewrite it. Because the main code does not need to be written again, inheritance can save time and effort.

Q: How to access any data member or methods of the parent class in Java?
Ans:

The super keyword can be used to access any parent class's data members or methods.

The super keyword is applicable at the variable, method, and constructor levels.
Syntax:

super.<method-name>();

Q: Is it possible for a class to extend itself in Java inheritance?
Ans:

No, a class cannot extend itself.

Q: What is the output of the program, when derived class extends base class?
Ans:

class Base
 {
 public void M1()
 {
 System.out.println(" Base Class Method ");
 }
 }
class Derived extends Base
{
 public void M2()
 {
 System.out.printIn(" Derived Class Methods ");
 }
}
class Test
{
 public static void main(String[] args)
 {
 Derived d = new Derived(); // creating object 
 d.M1(); // call Base Class Method 
 d.M2(); // call Derived Class Method 
 }
} 

Output:
C:\inheritance-program>javac Test.java

C:\inheritance-program>java Test
Base Class Method
Derived Class Method

Q: Why is Multiple Inheritance not supported in Java?
Ans:

A class in Java cannot extend more than one class. As a result, the following is disallowed.

public class Baz extends Foo, Bar{
}
Consider the above program, class Baz extends both class Foo and class Bar, and both classes Foo and Bar have the same method print(). The java compiler is now unable to determine which print method it should inherit. Multiple inheritance is not permitted in Java to avoid such a situation.

A class, on the other hand, can implement one or more interfaces, which has helped Java overcome the impossibility of multiple inheritances.

Q: What would be the output of the program, if we print n from the main class?
Ans:

class Foo
{
    int n = 50;
}

class Bar extends Foo
{
    int n = 10;
}

public class Baz
{
    public static void main(String[] args)
    {
        Foo p = new Bar();

        System.out.println(p.n);
    }
}

Output:

C:\inheritance-program>javac Baz.java
C:\inheritance-program>java Baz
50

Q: What would be the output of the program, if we make an object of derived class?
Ans:

class Foo
{
    {
        System.out.println("Called from Foo Class");
    }
}

class Bar extends Foo
{
    {
        System.out.println("Called from Bar Class");
    }
}

class Baz extends Bar
{
    {
        System.out.println("Called from Baz Class");
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        Baz baz = new Baz();
    }
}

Output:

Called from Foo Class
Called from Bar Class
Called from Baz Class












Recommendation for Top Popular Post :