Top 10 Interview Questions in Inheritance
In this blog lets discuss about the interview questions relevant to Inheritance
- What is Inheritance?
Inheritance is one important concept of OOPs. In Inheritance, one class can inherit the properties(instance variables) and behavior(methods) of another class. Few important terminologies that we use often are given below
Super Class: This is the class from which the properties(instance variables) and behavior(methods) are inherited. Also called as base class or parent class
Sub Class: This is the class which inherits the properties and behavior from another class. Also called as derived class or child class. The sub class can access all the methods and variables except the private members. It also can have its own instance variables and methods.
extends: Use the keyword extends to extend a class. The syntax is as follows
class Manager extends Employee
- Why do we need inheritance ?
Inheritance supports code reusability.
Assume we have three classes Vehicle,Car,Bike with similar properties as brand,model,price and method as void getMileage(). Instead of creating the instance variables in both the classes, we can add the variables in just one class which is generic – in this case Vehicle class. Then make the Car and the Bike class inherit the properties and methods of Vehicle class.
Remember while doing so, there should be an IS-A relationship between the classes.That is Car is a Vehicle, Bike is a Vehicle.
Let me talk about a wrong scenario. Assume we have two classes as Employee and Student with similar properties as name,age,mobileno, city and method as void printDetails(). Is it right to apply inheritance here?. Try applying IS-A rule. Student is an Employee or Employee is a Student. Oh, that sounds weird. So don’t apply inheritance
Remember the rule – Inheritance means IS-A relationship and is used for code reusability
- What type of inheritance does java support?
Java supports simple, multilevel and hierarchical inheritance.
Simple: This is one level of inheritance. One class inherits from another class. In this case, class Car inherits from class Vehicle – class Car extends Vehicle
Multilevel: This type of inheritance goes down for multiple levels –
class Car inherits from FourWheeler, class FourWheeler inherits from class Vehicle
class Vehicle is the sub class of Object class. So, automatically class Car can access the properties of Vehicle class.
Hierarchical: Multiple classes can inherit from a single class or A class can have multiple subclasses. In this case, Bike,Car,Truck are sub classes of Vehicle.
- What is the super class for all the classes?
Object class from java.lang package is the super class for all the classes.
- Explain inheritance with an example.
Here is an example for inheritance. I have two classes – Employee and Manager. Employee is the super class with name,empId as instance variables, void printDetails() as method.
Manager is the subclass with salary as instance variable. It uses the properties name,empId of the Employee class. Manager has its own method – double calcBonus(double amount).
Let us create a main class Demo with main() method. Create an object of Employee and you can call printDetails() method. Create object of Manager. Using manager object you can call the methods of Manager and also of the super class Employee.
- What is the use of super keyword?
‘super’ keyword should be always in the first line of any constructor. If it is not added by you, it will added automatically by the compiler. The purpose of super is to call and initialize the super class instance variables first. Always the super class instance variables are initialized first and then the subclass variables are initialized.
Remember the parameters within super() should match atleast one constructor of the super class.
- Can a class extend more than one class?
No. Java does not support multiple inheritance. Please refer question-3. However, whatever classes you create will become a sub class of Object class
- Can I access the sub class methods using a super class object?
No. You cannot access the subclass methods/properties using the super class object.Only the sub class object can access the properties of the super class.(one real example – The son/daughter can access the fathers’ mobile. But the father cannot access the son/daughters’ mobile)
- What is the difference between Inheritance and Association?
- What is the difference between Inheritance and Polymorphism?
Inheritance and Polymorphism complement each other. Runtime Polymorphism, which also called as Overriding happens with inheritance support. That is overriding happens only in a super class, subclass scenario.
If you have some more questions relevant to Inheritance, then do share with us. Also learn about the top questions and answers in overriding.