Tuesday 2 April 2024

Object Oriented Programming In Java Questions And Answers -2 (Inheritance)

 What is inheritance? 

  • Inheritance is a very powerful feature of OOPs. 
  • It is amechanism of acquiring properties or behaviors of existing class to a new class. 
  • In other words, A class inherits data fields or methods from another class. 
  • This helps to write redundant free code and also reduces code length.
  • For eg. Animal is a class and other classes like Dog, cat etc. can inherit the common properties of animal class.

Why we use inheritance? 

Inheritance provides following benefits -

    • Code reusability

    • Less development and maintenance costs

    • Reduces code redundancy and supports code extensibility

    • Save time and effort to write codes

What is superclass?

  • Super class is that class which is inherited by other class.
  • In other words, super class is a class from which sub class inherits the properties and behaviors. 
  • It is also called base class or parent class. 

What is a subclass? 

  • Sub class is that class which inherits other class. 
  • In other words, sub class is a class which inherits the properties and behaviors of super class. 
  • It is also called child class or derived class. 

What are the types of inheritance? 

Inheritance have following types -

• Single inheritance

• Multiple inheritance

• Multilevel inheritance

• Hierarchical inheritance

• Hybrid inheritance 

What is single inheritance? 

  • When a class inherits properties and behavior of only one class then this type of inheritance is called single inheritance.
  • In other words, in single inheritance there is only one base class and only one sub class. 

What is multilevel inheritance?

  • When a class inherits other class and then that inherited class further inherited by other class, this type of inheritance is called multi level inheritance.
  • In multilevel inheritance , sub class contains the properties of the base class as well as the properties of base class of its own class.

What is multiple inheritance?

  • When a class inherits the properties and behaviors of more than one class and then this type of inheritance is called multiple inheritance.
  • In multiple inheritance, there is only one sub class but more than one super class.
  • Java does not support multiple inheritance.

What is hierarchicachical inheritance?

  • When more than one class inherits the properties or behavior of only one class then this type of inheritance is called hierarchical inheritance.
  • In hierarchical inheritance , there are only one parent class but many child class.

What is hybrid inheritance?

  • Hybrid inheritance is a combination of more than one type of inheritance.

Why Java doesn't support multiple inheritance? 

Java doesn't support multiple inheritance because of following reasons -

  • Ambiguity Around The Diamond Problem
  • Multiple inheritance does complicate the design and creates problem during casting, constructor chaining etc.
  • So to simplify the language and reduce code complexity, Java doesn't support multiple inheritance. 

What is the difference between multiple and multilevel inheritance?

Multiple Inheritance

    1. In multiple inheritance, one sub class inherits the properties and behaviors of more than one super class.

    2. It has two class levels namely, base class and derived class.

    3. It inherits from the base class.

Multilevel Inheritance

        1. In multilevel inheritance, a class inherits other class and then that inherited class further inherited by other class.

        2. It has three class levels namely, base class, intermediate class and derived class.

        3. It inherits from the derived class.

What are the limitations of inheritance?

    • Decreases Execution Speed - Inheritance execution takes time and effort so it decreases the execution speed of code.

    • No Independence - Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

    • Uncertainty of results - Improper use of inheritance may lead to wrong solutions.

    • Memory wastage - Often, data members in the base class are left unused which may lead to memory wastage 


No comments:

Salesforce AI Associate Certification - 3

What is semantic retrieval in the context of LLMs?   Searching for relevant information in other data sources What additional protection do...