Three characteristics of OOP: Inheritance
Tags: java, computerscience, beginners, internship
Published: 2022-01-28 02:28:30

###Inheritance : W W H


**1. Defination (What):**
Key word: `extend`
 
When multiple classes have common attributes (member variables) and behavior (member methods), these common parts are extracted and defined in a public class. Other classes and classes can form an inheritance relationship with this public class, so that there is no need to redefine the public part in multiple classes! This common class is the parent class, also known as the superclass or base class, and the other classes are the subclasses. Subclasses can access non-private member variables of the parent class directly, and private member variables of the parent class can be accessed using the super.get() method.

Now we have the codes at the following:
```java
class Father{
    String car = "Toyota";
    String books = "Harry Potter";
}

class Son extends Father{ // son extends from his father
    
}

public class Test {

    public static void main(String[] args) {
        Father Howard_Stark = new Father();
        Son Tony_Stack = new Son();
        
        System.out.println(Tony_Stack.car);
    }
}
```

Seems like son has nothing. However, once the son extends from his father, Tony now could drive the car from his father and read the books, which his father "confer" to him.

```java
public class Test {

    public static void main(String[] args) {
        Father Howard_Stark = new Father();
        Son Tony_Stack = new Son();

        System.out.println(Tony_Stack.car); // output: Toyota
        System.out.println(Tony_Stack.books);// output: Harry Potter
    }
}
```

Some features that you should pay attention about the Inheritance in Java:

1. Every sub class is prowerful than base class, which means sub class would have or cover more vars and funcs than father class has. (Of course, Iron man Tony is much more greater than his father Howard haha)

2. Even son extends from his father, son could not "change" the feature of his father. The reason why son could not change his father's feature is that, father and son are two independent instances of `class Son` and `class Father`.

```java
public class Test {

    public static void main(String[] args) {
        Father Howard_Stark = new Father();
        Son Tony_Stack = new Son();

        Tony_Stack.car = "Mercedes";//Tony changes his car to Mercedes, but could not change Howard's car.

        System.out.println(Howard_Stark.car); // output: Toyota
        System.out.println(Tony_Stack.car); // output: Mercedes
    }
}
```
Hence, what Tony has in the future does not Howard's business, including his Mark One and Mark Two. That's why son class is absolutely greater than father class.

```java
class Father{
    String car = "Toyota";
    String books = "Harry Potter";
}

class Son extends Father{ // son extends from his father
    String suit1 = "Mark One";
    String suit2 = "Mark Two";
}
```

3. Java is single inheritance, not multiple inheritance (unlike C++). However, it is possible to build multi-level subclasses of inheritance.

Like Tony extends from Howard, Morgan Stark (Tony's daughter) extends from Tony, but Morgan could not extend from Howard and Tony at the same time.

Tony_Stark <-(extends)-- Howard_Stark
Morgan_Stark <-(extends)-- Tony_Stark

Morgan_Stark <--X-- Howard_Stark, Tony_Stark 



**2. Significance(Why):**
1. If a class want to get a same function from another class, it can set up an inheritance. (Save your time to coding, and increase the reusing rate of code)

2.Encapsulation and inheritance are the prerequisites for polymorphism implementation.

Of course, when you edit the feature of father class, the common features that the son has would be changed too, unless the son change these common features independently.


**3. Implementation(How):**

It is the best time to implement inheritance among classes:

1. When those classes have many properties in common.
2. When there are obviously relationship among those classes (like `class animals` includes `class dog` and `class cat`, so `class cat` and `class dog` extends the proteries from `class animals`).

After knowing when to use inheritance, you can try to build up the relationship and have a rough frame in your mind.

I'm going to talk about the six points of inheritance.


1.When construct an instance of sub class, the complier will invoke the constructor of base class first, and then invoke the constuctor of sub class.

```java
class Father{
    String car = "Toyota";
    String books = "Harry Potter";

    Father(){
        System.out.println("Father's constructor invoked");
    }
}

class Son extends Father{ // son extends from his father
    String suit1 = "Mark One";
    String suit2 = "Mark Two";

    Son(){
        System.out.println("Son's constructor invoked");
    }
}

public class Test {
    
    public static void main(String[] args) {
        Son Tony_Stack = new Son();
    }
}
```
Output:

>Father's constructor invoked
>Son's constructor invoked



2.`class Object` is the father class of any class in JAVA.

Hence, every instance (object) in Java is the polymorphism of instance of `class Object`.

3.The constructor of a subclass runs super () first, regardless of whether it has arguments or how many;

`super();` equals to default constructor of father class. So you can either explicitly or implicitly call `super()` in the any constructor of sub class.
```java
class Father{
    String car = "Toyota";
    String books = "Harry Potter";

    // where the super() is.
    Father(){
        System.out.println("Father's constructor invoked");
    }

    Father(String car){
        this.car = car;
    }
}

class Son extends Father{ // son extends from his father
    Son(){
        //super(); default
        System.out.println("Son's constructor invoked");
    }

    Son(String book){
        //super(); default
        this.books = book;
    }
}
```

4.Properties that are private to the parent class are not directly accessible to the subclass in test.java, even if the subclass inherits them. 

You can specify the subclass's parameter-constructor only by calling the parent's parameter-constructor. For properties that are not private to the parent class, the child class object can invoke directly.

```java
class Father{
    String car = "Toyota";
    String books = "Harry Potter";
    private String bank_account = "123456"; 
    Father(){
        System.out.println("Father's constructor invoked");
    }

    Father(String car){
        this.car = car;
    }
}

class Son extends Father{ // son extends from his father
    
    Son(){
        //super(); default
        System.out.println("Son's constructor invoked");
    }
    
}

public class Test {

    public static void main(String[] args) {
        Son Tony_Stack = new Son();
        Tony_Stack.bank_account = "111111";// error reported: 'bank_account' has private access in 'test.Father'!!!
    }
}
```

Due to the limitation of the paragraph, here are all my knowledge of Inheritance and, there will be more features' details in Polymorphism Section.

___

Your advice and comments are the power of my moving forward. Thank you for your reading.


***Never know how good you will be if you don't push yourself.***
Ricky Ruan:

E-mail: yruan@umass.edu