The three main characteristics of object-oriented model are inheritance, encapsulation, and polymorphism.
Let's talk about inheritance first. Inheritance is the continuation of the Child class on the parent class. All the child classes except the private part can be inherited by the Child class.
Inherited relationships:Subclass = inherited class = derived class, parent class = base class
The subclass that inherits the parent class is called the inheritance class. It can also be described as a subclass generated by the parent class derivation as a derived class.
The parent class inherited by the quilt class is called the base class.
Two inherited features:Singularity and transmission.
Singularity: single inheritance means that subclass inheritance can only inherit from one parent class.
Transmission: inheritance can be passed, that is, the inheritance class can become the base class of other classes.
The role of inheritance:Code (functional) can be reused and expanded.
Inheritance implementation:
class Demo1 { void show1(){} void show2(){} }class Demo2 { void show1(){} void show2(){} }
According to one of the functions of inheritance, code is reused. By observing the code above, we found that both the demo1 class and the demo2 class have the show1 property, so we can use inheritance.
class Demo { void show1(){} }class Demo1:Demo { void show2(){} }class Demo2:Demo { void show2(){} }
In this case, demo1 and demo2 both have the show1 () attribute.
You may say that there are just a few lines of code, so I am not afraid of the trouble. I just wrote something. But in reality, the problem is far from that simple. In large programs, there are many identical content and many functions need to be reused. If you say copy and paste are good at writing, you are not afraid of trouble, but the fatal problem is later. Any program requires maintenance, and maintenance may be modified. If inheritance is not required, the modification will become endless pain points.