JAVA: OOP (Inheritance)
Tags: java, oop
Published: 2020-12-12 09:56:03

Q1: [OOPS](https://dev.to/supriya2371997/java-oop-in-brief-4hlf): Inheritance?
Q2: When to use?
Q3: Advantages?
Q4: Disadvantages?
Q5: Explain types with example?
Q6: Facts about inheritance?
Q7: Why java does not support multiple inheritances?
Q8: How to access private members of the superclass?

****
**1. Inheritance:-**
>1.1 it is a mechanism of acquiring certain properties and behaviour of one class and another class i.e. parent class to child class.
1.2 the purpose of inheritance is to be able to create new classes that are built upon existing classes such as new child class on the existing parent class.
1.3 inheritance represents an IS-A-relationship i.e. parent-child relationship.
1.4 access modifiers play a major role in inheritance. Only public and protected members can be accessed by child class and not the private members. Although we can indirectly use private members using public or protected functions.
[Click here to view the code for 1.4 point](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/Getter_Setter.java)

**Syntax and terms used**
>class Subclass-name extends Superclass-name {  
>>  //methods and fields  
>}  

-: class - it is a group of objects with common properties. it is a template or blueprint from which object can be created.
-: subclass - class that inherits another class. It is also called a derived class, extended class, or child class.
-: superclass - class from where a subclass inherits the features. It is also called as a base class or a parent class.
-: extends - indicates the new class is inheriting from the existing class.

**Example** [Below source code](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/Single_inheritance.java)




![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7jhb1ygbi7z1y0ukzhu4.JPG)

**2. When to use:-**
>2.1 Method Overriding to achieve runtime polymorphism
2.2 Code reusability

**3. Advantages:-**
>3.1 reusability 
3.2 readability
3.3 reliability i.e. the base class code will be already tested and debugged
3.4 less costly because of reusability
3.5 supports code extensibility
3.6 improper use or less knowledge may lead to wrong solutions


**4. Disadvantages:-**
>4.1 less use of data members present in the base class may lead to memory wastage.
4.2 dependability may cause unintended damage i.e. inheritance causes the coupling between classes, thus a change in the base class will affect all the child processes.
4.3 can't inherit from final class i.e. below program will an error.
>final class Class1 { 
>>  ...
  }

>class Class2 extends Class1{
>>  ...
}

**5 Types:-**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jzkfz1waqgi2jn2oq34k.jpg)
1. Single Inheritance
>1.1 subclass extends or inherits the features of one superclass. eg. daughter class is inheriting father class
1.2 Basically, java only uses a single inheritance as a subclass cannot extend more superclass.
[CLICK HERE FOR SIMPLE PROGRAM](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/SingleInherit.java)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w9epjr4vg0hr66zplewe.JPG)

2. Multilevel Inheritance
>2.1 a class will be inheriting a base class and as well as the derived class also act as the base class to other class. eg. daughter inherit form father, and father inherit from grandfather
[CLICK HERE FOR SIMPLE PROGRAM](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/MultilevelInherit.java)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t6vcgjsiovh46z525b6o.JPG)

3. Hierarchical Inheritance 
>3.1 one class serves as a superclass for more than one subclass. eg. one teacher serves multiple students
[CLICK HERE FOR SIMPLE PROGRAM](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/HierarchicalInherit.java)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/23zssbgiz66307j40wxi.JPG)

4. Multiple Inheritance (can't be implemented using class in java)
>4.1 one class can have more than one superclass
4.2 java does not support multiple inheritances with classes, but it can only be achieved through interfaces
[CLICK HERE FOR SIMPLE PROGRAM](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/MultipleInherit.java)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h54tpudck2uj1twnt1d8.JPG)

5. Hybrid Inheritance
>5.1 combinations of two or more types of inheritance.
5.2 since multiple inheritances can't be achieved in java with classes, hybrid inheritance also can't be achieved in java with classes. But using inheritance only it can be implemented.
[CLICK HERE FOR SIMPLE PROGRAM](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/HybridInherit.java)

**6. Facts about inheritance:-**
>6.1 only one superclass: The superclass can only be one, that is because java does not support multiple inheritance with classes.
6.2 inherit constructor: derived class can inherit methods, variables and nested classes from its superclass but constructors, that is because constructors are not members. 
>>However, constructors of the superclass can be invoked from its derived class

>6.3 inheritance does not allow derived class to inherit private members of its superclass. 
>>However, it can be accessed using getters and setters in order to access them.

**7. Why java does not support multiple inheritances:-**
>7.1 To prevent ambiguity: consider a situation where both BaseClass1 and BaseClass2 has calculate() with different working or behaviour, then there will be ambiguity if ChildClass which inherits both the base classes will try to call calculate()
>>because java compiler can't decide which calculate() it should inherit.

**8. How to access private members of the superclass?**
>We can use getter and setter methods that 
[Click here for the code](https://github.com/supriya2371997/Java/blob/master/DEV%20COMMUNITY%20PROGRAMS/Getter_Setter.java)
>>option 1. return private member
option 2. return public member that store private member's values


**Please comment if you have any feedback or suggestions**