300+ [LATEST] Java Inheritance Interview Questions and Answers

Q1. Can An Interface Extends More Than One Interface In Java?

Yes, unlike classes, an interface can extend more than one interface in Java. There are several example of this behavior in JDK itself e.g. java.util.List interface extends both Collection and Iterable interface to tell that it is a Collection as well as it allows iteration via Iterator.

Q2. Why Inheritance Is Used By Java Programmers?

Inheritance is used for code reuse and leveraging Polymorphism by creating a type hierarchy. It’s better to use Inheritance for type declaration but for code reuse composition is a better option because it’s more flexible. See this article for learning more about why Composition is better than Inheritance.

Q3. How To Call A Method Of A Subclass, If You Are Holding An Object Of The Subclass In A Reference Variable Of Type Superclass?

  • You can call a method of the subclass by first casting the object hold by reference variable of  superclass into the subclass. Once you hold the object in subclass reference type, you can call methods from the subclass. See how type casting works in Java for more details.
  • That’s all about some good interview questions based OOP concept,  Inheritance. You should also know that how private and final variables affect Inheritance. How can you extend a class which is holding a private variable, and probably the difference between private and protected modifier in Java? They are really important to understand and use Inheritance in Java.
  • Also, if you think we have missed any important question related to Inheritance o If you come across any good questions based upon Inheritance or other object oriented concept then please share with us.

Q4. What Is Inheritance In Java?

Inheritance is an Object oriented feature which allows a class to inherit behavior and data from other class. For example, a class Car can extend basic feature of Vehicle class by using Inheritance. One of the most intuitive examples of Inheritance in the real world is Father-Son relationship, where Son inherit Father’s property. If you don’t know, Inheritance is the quick way to become rich.

Q5. Can A Class Extends More Than One Class In Java?

No, a class can only extend just one more class in Java. Through Every class also, by default extend the java.lang.Object class in Java.

Q6. What Is The Difference Between Polymorphism And Inheritance?

Both Polymorphism and Inheritance goes hand on hand, they help each other to achieve their goal. Polymorphism allows flexibility, you can choose which code to run at runtime by overriding.  See the detailed wer for more details.

Q7. Can We Override A Private Method In Java?

No,  you cannot override a private method in Java because the private method is not inherited by the subclass in Java, which is essential for overriding. In fact, a private method is not visible to anyone outside the class and, more importantly, a call to the private method is resolved at compile time by using Type information as opposed to runtime by using the actual object.

Q8. What Is The Difference Between Inheritance And Encapsulation?

Inheritance is an object oriented concept which creates a parent-child relationship. It is one of the ways to reuse the code written for parent class but it also forms the basis of Polymorphism. On the other hand, Encapsulation is an object oriented concept which is used to hide the internal details of a class e.g. HashMap encapsulate how to store elements and how to calculate hash values.

Q9. Can A Class Implement More Than One Interface In Java?

Yes, A class can implement more than one interface in Java e.g. A class can be both Comparable and Serializable at the same time. This is why the interface should be the best use for defining Type as described in Effective Java. This feature allows one class to play a polymorphic role in the program.

Q10. How To Use Inheritance In Java?

You can use Inheritance in Java by extending classes and implementing interfaces. Java provides two keywords extends and implements to achieve inheritance.  A class which is derived from another class is known as a subclass and an interface which is derived from another interface is called subinterface. A class which implements an interface is known as implementation.

Q11. What Is The Syntax Of Inheritance?

You can use either extends of implements keyword to implement Inheritance in Java.  A class extends another class using extends keyword, an interface can extend another interface using extend keyword, and a class can implement an interface using implements keyword in Java.

Q12. Why Multiple Inheritance Is Not Supported By Java?

Java is introduced after C++ and Java designer didn’t want to take some C++ feature which is confusing and not essential. They think multiple inheritances is one of them which doesn’t justify complexity and confusion it introduces. You can also check why multiple inheritances are not supported in Java for more reasons and discussion around this.

Q13. What Is The Liskov Substitution Principle?

  • The Liskov substitution principle is one of the five object-oriented design principles, collectively know as SOLID principles. This design principle is L of SOLID acronym. The Liskov substitution principle states that in an object oriented program if a function or method is expecting an object of base class then  it should work fine with a derived class object as well. If it cannot function properly with derived class object then the derived class is violating the Liskov Substitution principle.
  • For example, if a method is expecting a List you can also pass ArrayList or LinkedList and it should work just fine because ArrayList and LinkedList both follow Liskov Substitution Principle, but the java.sql.Date which is a subclass of java.util.Date in Java violates Liskov Substitution Principle because you cannot pass an object of java.sql.Date class to a method which is expecting an object of java.util.Date, Why? because all time-related method will throw java.lang.UnsupportedOperationException.
  • Here is another example of violating The Liskov Substitution Principle, Square is a special type of Rectangle whose adjacent sides are equal but making Square extending Rectangle violates LSP principle. For more details on SOLID design principles, read Clean Code by Rober C. Martin, the inventor of SOLID principles.

Q14. What Will Happen If A Class Extends Two Interfaces And They Both Have A Method With Same Name And Signature?

In this case, a conflict will arise because the compiler will not able to link a method call due to ambiguity. You will get a compile time error in Java.

Q15. What Is Method Hiding In Java?

Since the static method cannot be overridden in Java, but if you declare the same static method in subclass then that would hide the method from the superclass. It me, if you call that method from subclass then the one in the subclass will be invoked but if you call the same method from superclass then the one in superclass will be invoked. This is known as method hiding in Java.

Q16. Can We Override Static Method In Java?

No, you cannot override a static method in Java because it’s resolved at compile time. In order for overriding to work, a method should be virtual and resolved at runtime because objects are only available at runtime. This is one of the tricky Java questions, where interviewer tries to confuse you. A programmer is never sure about whether they can override or overload a static method in Java.

Q17. Can We Overload A Static Method In Java?

Yes, you cannot overload a static method in Java. Overloading has nothing to do with runtime but the signature of each method must be different. In Java, to change the method signature, you must change either number of arguments, type of arguments or order of arguments.