3

Top 10 Interview Questions in Inheritance

[Social9_Share type="horizontal"]

Top 10 Interview Questions in Inheritance

In this blog lets discuss about the interview questions relevant to Inheritance

  1. What is Inheritance?
  2. 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

  3. Why do we need inheritance ?
  4. 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

  5. What type of inheritance does java support?
  6. Java supports simple, multilevel and hierarchical inheritance.

    types of inheritance


    Simple: This is one level of inheritance. One class inherits from another class. In this case, class Car inherits from class Vehicleclass 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.

  7. What is the super class for all the classes?
  8. Object class from java.lang package is the super class for all the classes.

    Become an expert Java Developer

  9. Explain inheritance with an example.
  10. 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.

    Employee class

    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).

    Manager class

    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.

    Demo class

  11. What is the use of super keyword?
  12. ‘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.

  13. Can a class extend more than one class?
  14. No. Java does not support multiple inheritance. Please refer question-3. However, whatever classes you create will become a sub class of Object class

  15. Can I access the sub class methods using a super class object?
  16. 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)

  17. What is the difference between Inheritance and Association?
  18. Inheritance means IS-A relationship. Association means HAS-A relationship. For example Car is a Vehicle is Inheritance. Car has an Engine property becomes Association.Let me show this as java code

  19. What is the difference between Inheritance and Polymorphism?
  20. 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.

    Happy Learning

Post a comment

{"wp_error":"cURL error 28: Resolving timed out after 5514 milliseconds"}