Table of contents
1.
Java Inheritance Interview Questions and Answers
1.1.
1. What does Java's inheritance mean?
1.2.
2. What are superclass and subclass, respectively?
1.3.
3. How does Java implement or achieve inheritance?
1.4.
4. Write the syntax for a class's subclass creation.
1.5.
5. Can a class extends on its own?
1.6.
6. Are the instance initialization block and function Object() { [native code] } inherited by subclasses?
1.7.
7. In Java, are static members passed down to subclasses?
1.8.
8. Can you override a final method?
1.9.
9. How are constructors called in the case of inheritance?
1.10.
10. Why do Java programmers use inheritance??
1.11.
11. What benefits does inheritance offer in Java?
1.12.
12. What kinds of inheritance are there in Java?
1.13.
13. Why is inheritance necessary?
1.14.
14. Is the code going to compile successfully? In that case, what is the result?
1.15.
15. Why isn't Java's class-based multiple inheritance support available?
1.16.
16. What does Java's Super Keyword mean?
1.17.
17. What does Java method overloading mean?
1.18.
18. What are the Java guidelines for overloading a method?
1.19.
19. What does Java's method overriding mean?
1.20.
20. What does Java's method hiding mean?
1.21.
21. Is it possible to block method overriding without the final modifier?
1.22.
22. Does Java permit multiple inheritance or can a class extend multiple classes? Why not, then?
1.23.
23. List the Java access specifiers that are available?
1.24.
24. What does "co-variant Method Overriding" means?
1.25.
25. What distinguishes composition from inheritance?
1.26.
26. What distinguishes inheritance from encapsulation?
1.27.
27. What distinguishes overriding from overloading methods?
1.28.
28. What are the super() and this() methods in Java?
1.29.
29. Three classes, C1, C2, and C3, have been established in the code below. Both Class C2 and Class C1 are extended by Class C3.
1.30.
30. What distinguishes aggregation and composition as two distinct concepts?
2.
Conclusion
Last Updated: Feb 3, 2025
Easy

Top 30 Java Inheritance Interview Questions and Answers

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

The process of deriving a new class from an existing class is known as inheritance. The methods and properties of other classes may be inherited or acquired by a class in Java. Subclasses are classes derived from other classes, whereas superclasses are those derived from other classes.

Java Inheritance Interview Questions and Answers

Let's discuss the top java inheritance interview questions with their detailed solutions.

Want to know about the Fibonacci Series in Java in detail.

Java Inheritance Interview Questions and Answers

1. What does Java's inheritance mean?

Inheritance is the term used in Java to describe the process of building a new class utilizing the features of an existing class. In other words, inheritance is how a child class acquires each parent class's traits.

2. What are superclass and subclass, respectively?

Superclasses are classes from which features are inherited by subclasses. It is also known as the parent class or foundation class.

Subclasses are classes that contain all the properties, methods, and nested classes of another class. A derived class, kid class, or extended class are other names.

3. How does Java implement or achieve inheritance?

Two keywords can be used to implement or accomplish inheritance:

  • extends: The keyword extends is used to create an inheritance relationship between two classes and two interfaces.
  • implements: The term "implements" establishes the line of descent between a class and an interface.

4. Write the syntax for a class's subclass creation.

The "extends" keyword can be used to build a subclass. The following syntax is used to declare a class subclass:

class subName extends superName
{
  // SubclassVariables
  // SubclassMethods
}
You can also try this code with Online Java Compiler
Run Code

5. Can a class extends on its own?

Unable to extend itself.

6. Are the instance initialization block and function Object() { [native code] } inherited by subclasses?

No, the superclass's constructor and instance initialization block cannot be passed down to its subclass; nonetheless, they are used when creating an object of the subclass.

7. In Java, are static members passed down to subclasses?

Static blocks can't be passed down to their subclasses.

A static method from the superclass is inherited as a static member by the subclass, while non-static methods are only inherited as non-static members.

8. Can you override a final method?

A final method cannot be overridden, unfortunately.

9. How are constructors called in the case of inheritance?

When there is inheritance, constructors are called in a hierarchical order.

10. Why do Java programmers use inheritance??

An inheritance is utilized to leverage polymorphism and reuse code by establishing a type hierarchy. For type declaration, inheritance is preferable, but because the composition is more versatile, it's a better choice for code reuse.

11. What benefits does inheritance offer in Java?

The following are some benefits of inheritance in Java:

  • By placing the common code in the superclass and distributing it among various subclasses, we can reduce the amount of duplicate code in an application.
  • The application's redundancy is decreased as a result of shorter code.
  • Application code may become more adaptable through inheritance.

12. What kinds of inheritance are there in Java?

The different inheritance kinds are as follows:

a. Multi-level Inheritance

b. Single Inheritance

c. Multiple Inheritance 

d. Hybrid Inheritance and,

e. Hierarchical Inheritance

Know about Single Inheritance in Java in detail.

13. Why is inheritance necessary?

One of the fundamental elements of the OOPs paradigm is inheritance. Some objects have similar traits and characteristics. A child class can inherit all of the traits and actions of the parent class.

The usage of inheritance in Java is justified for the reasons listed below.

  • The basic class's code is reusable.
  • By overriding, we can extend the functionality of a class or method via inheritance.
  • The features of a class that already exist are utilized through inheritance.
  • It is used to implement method overriding, often known as runtime polymorphism.

14. Is the code going to compile successfully? In that case, what is the result?

public class C1 { 
   int x = 50;
}
public class C2 extends C1 {
   int x = 40;
}
public class Check {
public static void main(String[] args) 
{
   C2 c2 = new C2();
   System.out.println(c2.x); 
 
   C1 c1 = new C1();
   System.out.println(c1.x);
 
   C1 c3 = new C2();
   System.out.println(c3.x);
  }
}
You can also try this code with Online Java Compiler
Run Code

Yes, the compilation of the code will succeed, and 40, 50, 50 is the output.

15. Why isn't Java's class-based multiple inheritance support available?

Multiple Inheritance in Java refers to when a class extends two base classes or superclasses, although a class cannot simultaneously extend more than one class in Java. One class may extend no more than one other class.

Because of this, Java does not permit multiple inheritance through classes, which reduces ambiguity, complexity, and confusion.

16. What does Java's Super Keyword mean?

A reference variable used to refer to the immediate parent class object in Java is the super keyword.

When you create an instance of a subclass, an implicit instance of the parent class is also produced and referred to by the super reference variable.

Three objectives serve the super keyword:

  • The term "super" refers to an instant parent class variable.
  • The instant parent class constructor is called using the super() function.
  • The immediate parent class function is called using super.

17. What does Java method overloading mean?

The act of declaring two methods with the same name but different method signatures is known as overloading.

For example, System.out, an object of the PrintStream class, contains several println() methods that can be used to print various data types, including byte, short, int, char, float, and double.

They are all referred to as overloaded methods. In Java, overloaded method calls are resolved at compile time and require distinct method signatures.

18. What are the Java guidelines for overloading a method?

The only requirement for method overloading in Java is that each overloaded method's method signature is unique.

For example, the method signature can be altered by altering the amount or type of method arguments. System.out.println() is overloaded to accept various primitive types, including int, short, byte, float, and others.

All of them accept the same reasoning, but they all have different types.

Changing the order of the method arguments is another way to change the method signature; however, doing so frequently results in unclear code and should be avoided.

19. What does Java's method overriding mean?

Another option to define a method with the same name but a different code is to override it; this method must be in a subclass.

The basis for overriding is run-time. Method calls with polymorphism are resolved at runtime based on the actual object.

For example, if a variable of type Parent contains an object of the Child class, the method called will, unless it is overridden, be from the Child class and not the Parent class.

You must go by the rules of method overriding, which include declaring a method with the same signature in a subclass to override a method.

20. What does Java's method hiding mean?

Because Java's static methods' method calls are resolved at compile time, they cannot be modified. However, this did not stop you from declaring a method with the same name in a subclass.

In this instance, we say that a static method in the parent class was hidden by a method in the subclass.

Because overloading is handled at compile time, if a variable in the parent class points to an object in the child class, the parent class's static method will also be called.

Let us now discuss the hard type of java inheritance interview questions.

21. Is it possible to block method overriding without the final modifier?

In Java, there are indeed some odd ways to prevent method overriding. Although the final modifier is only for that purpose, method overriding can be avoided using the private keyword.

How? If you recall, an extensible class is required to override a method. Because the parent class's constructor won't be available to subclasses if you make it private, that class cannot be extended.

This cannot be overridden because it is automatically called by the subclass's constructor.

This method is applied in the Singleton design pattern, where the constructor is purposely made secret, and an accessible singleton instance is made available via a static getInstance() method.

22. Does Java permit multiple inheritance or can a class extend multiple classes? Why not, then?

Java does not enable multiple inheritance, nor can a class extend more than one class. Java does not enable multiple inheritance since it can lead to ambiguity, complexity, and confusion. For example, Class C will have two methods with the same name if it extends Classes A and B, each of which has a method with the same name. This makes it unclear and difficult to decide which approach to take. Java does not permit multiple inheritance in order to prevent this.

class C1
{
    void funOne()
    {
        System.out.println("From methodOfClassC1");
    }
}
 
class C2
{
    void funOne()
    {
        System.out.println("From methodOfClassC2");
    }
}
 
class C3 extends C1, C2 ()
{
    //Class C3 will inherit two identical methods.
 
    //This leads to uncertainty and misunderstanding.
}
You can also try this code with Online Java Compiler
Run Code

23. List the Java access specifiers that are available?

Here is a list of the access specifiers that Java supports:

  • private: A private modifier is only accessible to members of the class. Outside of class, it can't be accessed.
  • default: A default modifier is only available within the package. Outside of the package, it cannot be accessed. If no access level is specified, the default will apply.
  • protected: A protected modifier can be accessed inside and outside the package via a child class. Only a child class can access it; nevertheless, it cannot be accessed outside the package.
  • public: A public modifier is available everywhere. It may be accessible both inside and outside of the class and inside and outside the package.

24. What does "co-variant Method Overriding" means?

One of the rules of method overriding is that the return type of the overriding method must be the same as the overridden method; however, starting with Java 1.5, this requirement has been slightly eased and the overridden method can now return a subclass of the return type of the original method.

Casting at the client end can be eliminated thanks to a relaxation technique called co-variant method overriding.

The clone() method overriding is one of the best examples. The Object.clone() method returns an Object that needs to be cast, but by overriding the co-variant method, you can return the appropriate type immediately.

Example: The Date class returns an object of type java.util.Date rather than java.lang.Object.

25. What distinguishes composition from inheritance?

In Java, composition and inheritance differ in a number of ways, including the following:

  • While inheritance cannot be modified, i.e. you cannot ask a class to implement another class at runtime, the composition is more flexible since you may change the implementation at runtime by executing the setXXX() function.
  • A Room HAS A Fan, yet a Mango IS-A Fruit is an example of how composition builds HAS-A relationship while inheritance builds IS-A relationship.
  • Inheritance represents the parent-child relationship the best. However, the composition can also be used if you just need one class. 

You can also read about Has-A Relationship in Java here.

26. What distinguishes inheritance from encapsulation?

A parent-child relationship is created via the object-oriented idea of inheritance. Although it serves as the foundation for polymorphism, it is one approach to reuse code created for parent classes.

On the other hand, encapsulation is an object-oriented notion used to conceal a class's internal details, such as How to store elements and generate hash values are both covered by HashMap.

27. What distinguishes overriding from overloading methods?

The primary distinction between overloading and overriding is that the former occurred during compile time while the latter occurred during run time. This is the cause.

Only virtual methods in Java can be overloaded. Methods resolved at compile time, such as private, static, and final methods in Java, cannot be overridden.

Additionally, the conditions for overloading and overriding methods differ. For instance, a method must have a different method signature to be considered overloading, whereas it must have the same method signature to be considered overriding.

28. What are the super() and this() methods in Java?

super()

  • To call the constructor in the superclass, use the super keyword.
  • Super always refers to the current class's parent.
  • You can use Super to access the parent class's public and protected methods and properties. The parent's private methods and properties are hidden from you.
  • Only the constructors of the class can access constructors using super.

this()

  • A reference to the current class is made by this.
  • It enables access to the current class's methods and attributes, including private ones.
  • It is used to access the current object's methods and fields. Because of this, it, for instance, has no significance in static methods. this keyword was previously used to call the class's constructor (other overloaded constructor)

29. Three classes, C1, C2, and C3, have been established in the code below. Both Class C2 and Class C1 are extended by Class C3.

Is there a way for Class C3 to invoke the method f1() of Class C1, which exists in each class?

public class C1 
{
  void f1(){
    System.out.println("f1 in class C1");  
 }
}
public class C2 extends C1 
{
  void f1() {
     System.out.println("f1 in class C2");
 }
}
public class C3 extends C2 
{
  void f1() {
System.out.println("f1 in class C3"); 
   }
 }
public class Test {
public static void main(String[] args) 
{
   C3 c3 = new C3();
    c3.f1();
  }
}
You can also try this code with Online Java Compiler
Run Code


Every class in the code above has a method called f1() with the same signature; as a result, Class C2 overrides Class C1's f1() method, and Class C3 overrides Class C2's f1() method.

Using the super.f1() call classes C2 and C3 can now call their superclass's f1() function.

However, in this case, invoking C1's f1() method from Class C3 is not possible because it violates Java's OOPs notion and does not employ super.super.

Since multiple inheritance is not permitted in Java, C3 can only see one superclass, which will have just one f1() method implementation. The f1() method of C1 is hidden from C3.

This situation is sometimes referred to as the Diamond Problem of Multiple Inheritance. Class C2 and Class C3 must both call the super.f1() method for class C3 to call class C1's f1() implementation.

30. What distinguishes aggregation and composition as two distinct concepts?

The terms "aggregation" and "composition" refer to different association links in the OOPs paradigm. Between classes, a composition forges a solid connection.

All components of a composite item are destroyed if it is destroyed. A car, as an illustration, has a steering wheel. The steering wheel's presence is meaningless if the car is destroyed.

A weaker association between classes is established via aggregation than by composition. As an illustration, libraries have students. Students still exist even if a library is destroyed. As a result, the library and the student are related.

Conclusion

In this article, we have learned about the top Java inheritance interview questions and answers. This article will help you to prepare for your interview and crack it easily. You can also check out our other blogs as well for interview preparation.

Recommended Readings:

Live masterclass