Introduction to Java Inheritance Interview Questions And Answers
Inheritance is the major concept of object-oriented programming like Java by which we can inherit the properties like methods, attributes from one class to another class. A class which is derived from one class is called subclass and a class from which a subclass is derived is called superclass. At any time a subclass can have only one superclass whereas superclass can have more than one subclass. A subclass can inherit the properties like attributes, methods from the superclass. Let us consider an example vehicle is a superclass whereas car, motorcycle, lorry etc are subclasses whereas car, motorcycle, lorry can inherit the properties of the vehicle and inheritance is very useful in programming and code re-use.
There are some limitations in the inheritance also like subclass cannot inherit the properties from the superclass if they are declared with private scope and constructor, initializer cannot be inherited by a subclass and each subclass can only have one superclass only. Java Inheritance is the most important topic in object-oriented programming and important in programming related interview questions. So we will be having a brief look on inheritance interview questions in different ways and contexts.
Now, if you are looking for a job which is related to Java Inheritance then you need to prepare for the 2019 Java Inheritance Interview Questions. It is true that every interview is different as per the different job profiles. Here, we have prepared the important Java Inheritance Interview Questions which will help you get success in your interview.
In this 2019 Java Inheritance Interview Questions article, we shall present 10 most important and frequently used Java Inheritance Interview Questions. These interview questions are divided into two parts are as follows:
Part 1 –Java Inheritance Interview Questions (Basic)
This first part covers the basic Java Inheritance Interview Questions and Answers.
Q1. What is the difference between Inheritance and composition in Java?
Java supports both composition and inheritance but both are different in many ways. Composition objects have a reference to the composition classes by having a loosely bounded relationship and it has a – has a relationship between classes. Using this we can use single classes can be composed within multiple classes and used in dependency injection and composition is a relationship between objects whereas Inheritance is a relationship between the classes and it has is a relationship between the classes and using inheritance derived class object carries the base class definition hence it is a tightly bounded relationship. It is mostly used in run time polymorphism and in this a single class can only inherit only one class.
4.8 (2,981 ratings)
Q2. How many types of inheritances available in Java programming language?
In java programming language there are five types of Inheritances as below:
Single inheritance, multilevel inheritance, hierarchical inheritance, hybrid inheritance, and multiple inheritances.
- Single Inheritance: In this one class is inherited or extended by only one class only.
- Hybrid Inheritance: Hybrid inheritance is a combination of Single, Multilevel and hierarchical inheritances.
- Multilevel Inheritance: In multilevel inheritance, one class is extended by one class and that extended class or the subclass is being extended by another class and forms a chain of the relationship between the classes is called multilevel inheritance.
- Hierarchical Inheritance: In this inheritance, one class is being extended more than one class.
- Multiple Inheritance: In this inheritance, one class extends more than one classes and it is not being supported by Java.
Let us move to the next Java Inheritance Interview Questions.
Q3. What is Super Keyword in Java?
The super keyword in Java is a reference variable to the immediate parent class object i.e whenever an instance of a subclass is created, an instance of the parent class is created implicitly which means referred by the super keyword or super reference variable. The super keyword has different purposes and uses as a super keyword can be used to refer immediate parent class variable, super() is used to refer immediate parent class constructor and super is used to invoke immediate parent class method.
Q4. What is the difference between Inheritance and Encapsulation?
This is the common Java Inheritance Interview Questions asked in an interview. Inheritance is a relationship between classes and object-oriented concept. It is useful to implement the responsibility of the code while programming. We can use the properties of the base class by extending to the derived class or subclass which also have the basis of polymorphism. Whereas Encapsulation is also an object-oriented concept in Java which is being used to hide the internal details of a class like methods, attributes. It has only declaration visible but not the definition like a Hash-map encapsulates how to store and calculate the hash values.
Q5. What is Method overloading in Java?
Method overloading in Java is defined as two methods are declared with the same name but with different signatures such as one method may accept two parameters whereas another method may accept only one parameter or three parameters etc. For example System.out.println is an overloading method in Java as we able to print different data types using it and method overloading will be resolved during compile time in Java.
Part 2 –Java Inheritance Interview Questions (Advanced)
Let us now have a look at the advanced Java Inheritance Interview Questions and Answers.
Q6. What is method Overriding in Java?
Method overriding in Java is defined as there will be two methods with same name and signature but code or logic is different and both methods need to be in the same subclass. It is based on run-time polymorphism as method calls are resolved during run-time in Java based on the actual object.
Q7. Is it possible to override a private method in Java?
No, we cannot override a private method in Java as private methods scope is limited to that particular class only and they are not visible outside of that class, so they cannot be visible in derived class or subclass also. So the private methods are not overridden.
Let us move to the next Java Inheritance Interview Questions.
Q8. Rules of method over-riding in Java?
The rules of method overriding in Java is overriding method can’t throw a higher exception than overridden method and it is applicable or true for the checked exceptions and overriding method cannot change the scope of the overridden method i.e if a method is public in base class it should be public in subclass also.
Q9. In Java, Constructor over-riding is possible?
This is the most popular Java Inheritance Interview Questions asked in an interview. In Java, Constructor overriding is not possible as the constructors are not inherited as overriding is always happens on child class or subclass but constructor name is same as a class name so constructor overriding is not possible but constructor overloading is possible.
Q10. Can a class can implement more than one interface in Java, is it possible?
In Java, Yes a single class can implement more than one interface. A class can be sterilizable and comparable at the same time in Java. This is one of the reasons that interface is mostly used in programming for this purpose and we can use a class as a polymorphic role while programming.
Finally, It’s a conclusion of the Java Inheritance interview questions. I hope you are preparing well for the interview and this Java Inheritance Interview Questions article will help you further in your interview preparation. I would like you all the highest for your Interview preparation and Interview.
This has been a guide to the list of Java Inheritance Interview Questions so that the candidate can crackdown these Java Inheritance Interview Questions easily. Here in this post, we have studied top Java Inheritance Interview Questions which are often asked in interviews. You may also look at the following articles to learn more.