Java is one of the most popular programming languages out there. A lot of people attend Java interviews every day and while the level of questions always depends on the expertise, people usually confuse while giving answers to come common questions. Today, I will share answers to some most common Inheritance questions asked in Java interviews.
Read:Â 5 Best Open Source IDEs for Java Programming Language
Contents
Inheritance Interviews Questions with Detailed Answers
1. What’s the Use of Inheritance?
Inheritance is an important pillar of object-oriented programming. It’s a mechanism that allows a class to inherit the properties of another class. As you might know, in the case of inheritance, classes are extended whereas interfaces are implemented. But the difference is — an interface extends an interface and a class implements an interface.
The class or interface which is extending another class or interface is called subclass, a base class or child class, whereas the class which is being extended and whose properties are being used by another class is called superclass.
2. Why You Can’t Inherit a Constructor?
There are many Java enthusiasts out there who remain confused about the inheritance of a constructor. But if you try to understand this question carefully, it won’t be difficult to answer.
we can’t inherit the properties of a constructor simply means we cannot create a subclass instance using any of the superclass constrictors. You cannot do this because you do not want the properties of a superclass constructor to be overridden.
When we say that we can’t inherit a constructor, it means that a subclass instance cannot be created by making use of any of the superclass constrictors. You cannot do this just because you do not want the properties of a superclass constructor to be overridden. This would have been possible if just talk about the inheritance but not because doing that would arise conflicts with another concept known as Encapsulation.
Also Read:Â 5 Important Tips to Become a Good Java Developer
3. Does Java Support Multiple Inheritance?
A class cannot extend more than one class in Java and hence it doesn’t support multiple inheritances. However, there are some workarounds to get rid of this limitation. A class can implement one or more interfaces. You can declare the extend keyword only once, and then declare the parent interfaces in a comma-separated list. This will help you achieve something not officially supported by Java. For better understanding, check the example given below:
Example
interface University { public void visit(); } interface College { public void enter(); } interface Classroom extends College, University{ public void search(); } public class Tester{ public static void main(String[] args){ Classroom Classroom = new Classroom() { public void visit() { System.out.println("visit University"); } public void enter() { System.out.println("enter College."); } public void search() { System.out.println("search Classroom."); } }; Classroom.visit(); Classroom.enter(); Classroom.search(); } } Output visit University enter College. search Classroom.
4. What will happen if Superclass and Subclass have the same Field Name?
It’s one of the frequently asked questions of Java Interviews. The answer would be the Superclass field will become hidden in the subclass. You can access the hidden superclass field using ‘super’ keyword in the subclass.
5. What is the Difference Between Inheritance and Encapsulation in Java?
Both Inheritance and Encapsulation are the building blocks of object-oriented programming. Inheritance is a way to reuse the code already written for the parent class whereas Encapsulation is used to hide the internal details of a class. Inheritance also forms the basis of Polymorphism but Encapsulation helps to maintain the readability of the code while ensuring the security as well.
Also Read:Â 10 Best Java Frameworks for Software Developers
Wrapping Up
I hope this article helped you prepare for your Java interview well. Inheritance is among the three principles of object-oriented programming and therefore, knowledge of inheritance is a must. If you want to add any other common Inheritance questions here, just use the comment section below.