Day 21 : Inheritance ( 1 )

Chi-Tse Chiang
2022-10-06 22:00:28

Intro

The next two days will introduce a very core concept of object-oriented programming: Inheritance

Inheritance is a method used in classes that simply means to add code while retaining the original content.

Since we mentioned inheritance, we can also cite the data structure that is also a very well-known concept: the binary tree (B tree).

Binary trees themselves use the concept of inheritance, with child nodes inheriting from their parent nodes, and object-oriented programming also uses a similar concept.

As we mentioned earlier, in Java, there is unidirectional inheritance:
a parent class can be inherited by multiple child classes; however, a child class cannot inherit 2 or more parent classes at the same time.

To sum up:

1. The original attributes and function variables of the parent class will be inherited by a class.

2. The child class will also add its own attribute variables, etc., which can be said to be an extension of the parent class.

3. However, we usually explain that the child class is an extension of the parent class.

Concept

Declare Inheritance Types

The keyword extends is used to declare that one class inherits from another.

class School{
	/*...........
     description
    ...........*/
}

class Student extends School{
	/*......................
     description in School
	......................*/

	/*............
	 description
	............*/
}

The child class (Student) inherits the content of the parent class (School) and adds a new program.
-> The last instruction for class inheritance is extends + parent class name.

When we want to use a member of the parent class in the main program, we can directly create an object of the child class to call it.
However, there are two situations:

1. private variables in the parent class

2. the parent class's constructor

These cannot be directly inherited by the child class. Private variables, for example, need to be accessed using the get and set concepts within the package.

Access Modifier Inheritance

Here we introduce the previously mentioned prefix modifier protected that can be inherited by the quilt category

class School{
	protected String name = "Chi";
    protected float warning;
	public void show_Sname(){
		System.out.println("Personal Information : " + name);
	}
}

class Student extends School{
	public void show_warning(int late){
        warning = late / 3;
		System.out.println("You hava " + warning + " warnings");
	}
}

public class Main{
	public static void main(String[] args){
	Student paper = new Student();
    int late = 21;
		paper.show_Sname();
		paper.show_warning(late);
	}
}

Program analysis:

Subclass (Student) inherits the variable functions in the parent class, and its variable modifier is protected. Its access permissions can be granted to subclasses and classes in the same package. Assuming that this is changed to the default mode, it cannot be inherited by subclasses.

In the main program, we only need to create an object of Student to use the properties inherited from the parent class.

This chapter focuses on what inheritance is and how to use it. The next chapter is also about inheritance, but it will introduce more advanced topics such as how private is inherited and explain different inheritance concepts using object-related keywords.