Inheritance of the Three Characteristics of Object-Oriented Programming (OOP)


Encapsulation of the Three Features of Object-Oriented Programming (OOP)
Polymorphism of the Three Features of Object-Oriented Programming (OOP)

foreword

"I will be there in 5 minutes. If not, read this again." I will be there in 5 minutes. If not, please read this message again. ——Daily life of a programmer

1. Inheritance

Since listening to the last class, Xiao Hei and Xiao Pang have a better understanding of Java object-oriented technology. After class, the two of them discussed in depth how to return a reference to a mutable object.

The conclusion is that the original object can be cloned first, and the object clone refers to a copy of the object stored in another location.
insert image description here

The classroom is very noisy. Today is the Lakers vs. Nets game. The students are very excited and have their favorite teams. When the goal was scored, cheers, shouts and complaints resounded throughout the classroom. At this time, Professor James Gosling in a plaid shirt pushed up his glasses and walked into the classroom, and the students gradually became quiet...

Xiao Hei and Xiao Pang sat in the front row again, but this time there was a young lady with beautiful blue eyes next to them. Exudes a charming fragrance…

Professor Gosling: Today, what I want to talk about is the second major feature of object-oriented programming—inheritance. Can anyone tell me what inheritance is?

Xiao Hei volunteered: Inheritance can be understood as elderly parents leaving millions of dollars to their children so that they can live a better life, or handing over the family business to their children for management. The process by which children get things from their parents is called inheritance.

Xiaopang added: I think it is also possible for children to acquire certain talents from their parents and have some unique abilities of their own. For example, Irving, the No. 11 player of the Nets, inherited his basketball talent from his father since he was a child.
insert image description here

Professor Gosling: Your words are very good, vivid and appropriate. In OOP, so-called inheritance refers to the relationship between classes in which a class shares the structure and behavior defined by one or more other classes. Inheritance describes the " is - a " relationship between classes. Subclasses can extend, override, and redefine the behavior of the base class. A subclass can replace a parent class anywhere a parent class can appear.

2. Simple example

An Animal class (superclass)

The code is as follows (example):

public class Animal{
    
    
	private int age;
	private String name;
	
	public Animal(int age, String name,) {
    
    
		this.age= age;
		this.name= name;
	}
	// 吃饭
	public void eat() {
    
    
		System.out.println(name + " 正在吃饭")}
	
	// 睡觉
	public void sleep() {
    
    
		System.out.println(name + " 正在睡觉")}
}

A Dog class inherits the Animal class
code as follows (example):

public class Dog extends Animal{
    
    
	private int age;
	private String name;
	
	public Dog(int age, String name) {
    
    
		this.age= age;
		this.name= name;
	}
	// 看门口
	public void watchDoor() {
    
    
		System.out.println(name + "正在看门口");
	}
}

A Cat class inherits the Animal class
code as follows (example):

public class Cat extends Animal {
    
    
	private int age;
	private String name;
	
	public Cat(int age, String name) {
    
    
		this.age= age;
		this.name= name;
	}
	// 捉老鼠
	public void catchMouse() {
    
    
		System.out.println(name + "正在捉老鼠");
	}
}

Since inheritance is an (is a) relationship, then we can understand well that a dog is an animal, and a cat is also an animal. And the subclasses Dog and Cat both inherit the eat() and sleep() methods from the parent class Animal, in addition, they also have their own unique methods. The keyword of the inheritance relationship is: extends , and the subclass can access the method of the parent class through the super keyword.

Note: The constructor of the subclass cannot access the private fields of the superclass. This part of the private field must be initialized through the constructor of the parent class. You can use super to call the constructor of the superclass, which must be written first in the subclass constructor. statement.


Advantages of inheritance:
1. It is easier to implement and improves the reusability and scalability of the code.
2. In a subclass, you can add domains, methods, or override superclass methods, but you cannot delete any inherited domains and methods. Generally, subclasses have more functions than superclasses; 3. Inheritance is the premise of polymorphism
;
4; The problem solved by inheritance is: commonality extraction. Put general-purpose methods in superclasses and special-purpose methods in subclasses.

Disadvantages of inheritance:
1. Inheritance is intrusive. Breaks the encapsulation, because the superclass exposes the implementation details to the subclass;
2. Increases the coupling relationship between classes. When the constants, variables, and methods of the superclass are modified, the modification of the subclass needs to be considered, and sometimes the modification requires refactoring;
3. Lack of flexibility and stability. The free space of the subclass is constrained by the superclass

Consider using inheritance only when the following conditions are met:
* The subclass is a special type, not just a role of the superclass;
* The instance of the subclass does not need to become an object of another class;
* The subclass extends, Instead of covering or invalidating the function of the parent class (Liskov substitution principle).


Before I knew it, the bell rang outside the classroom, and it was time to cook! The game enters overtime...Which team will win in the end?

insert image description here

3. Summary

For the use of inheritance, we also need to follow certain principles in order to better achieve the design goals of high cohesion and low coupling.

Supongo que te gusta

Origin blog.csdn.net/weixin_48627356/article/details/120931944
Recomendado
Clasificación