Polymorphism overview
- Polymorphism is the third characteristic of object-oriented after encapsulation and inheritance.
- For the practical significance and personal understanding of polymorphism:
- Polymorphism: The same reference calls the same method but does different things.
- Real things often reflect many forms, such as students, students belong to the category of people, and a specific classmate, Zhang San, belongs to both the category of students and people, that is, there are multiple forms. This is the real thing. Polymorphic.
- As an object-oriented language, Java can also describe multiple forms of a thing. If the Student class inherits the People class, the object of a Student is both Student and People.
-
Polymorphism is embodied in the parent class reference variable can point to the child class object.
Note: When using the polymorphic parent class reference variable to call the method, the subclass rewritten method will be called. -
Definition and usage format of polymorphism
定义格式:父类类型 变量名=new 子类类型();
-
understanding:
多态是同一个行为具有多个不同表现形式或形态的能力。 多态就是同一个接口,使用不同的实例而执行不同操作。
6. Some advantages of polymorphism:
Interface, polymorphism is that the superclass provides a common interface to the subclass through method signatures, and the subclass completes or overrides it to achieve;
replaceability, polymorphism can replace the existing The code;
scalability, polymorphism can add new subclasses without affecting the operation and operation of the polymorphism, inheritance and other characteristics of existing classes;
flexibility, polymorphism is very flexible;
fifth, simplicity, it It also simplifies the process of writing and modifying application software codes. .
Characteristics of polymorphic members
class People{
private int num=6;
int vis=666;
public void eat() {
System.out.println("米饭");
}
public void getNum() {
System.out.println(num);
}
}
class Student extends People{
private int num=10;
int vis=66;
public void eat() {
System.out.println("炸鸡");
}
public void study() {
System.out.println("good good study");
}
public void getNum() {
System.out.println(num);
}
}
public static void main(String[] args) {
People p=new Student();
System.out.println(p.vis);//
p.eat();
p.getNum();
//输出
//666
//炸鸡
//10
- Polymorphic member variables:
only the variable value of the parent class can be obtained - Polymorphic member method:
call the method overridden by the subclass, but it must be a method in the parent class
Upward transformation
Use format: parent type variable name=new subtype type();
Applicable scenario: when there is no need to face the subtype type, the corresponding operation can be completed by improving the extensibility or using the function of the parent class.
**Understanding:** Use the method in the parent class, but call the method overridden by the subclass
Downcast
Use format: subclass type variable name=(subclass type) variable of the parent type;
**Applicable scenario: when you want to use the unique function of the subclass. **
**Understanding: **Use subcategories to solve problems
Code sample display
package ParentAndBase;
class People{
private int num=6;
public void eat() {
System.out.println("米饭");
}
public void getNum() {
System.out.println(num);
}
}
class Student extends People{
private int num=10;
public void eat() {
System.out.println("炸鸡");
}
public void study() {
System.out.println("good good study");
}
public void getNum() {
System.out.println(num);
}
}
class Teacher extends People{
public void eat() {
System.out.println("拉面");
}
public void study() {
System.out.println("day day up");
}
}
public class Demo {
public static void main(String[] args) {
People p=new Student();//向上转型
p.eat();
p.getNum();
Student stu=(Student)p;//向下转型
stu.study();
stu.eat();
People pp=new Teacher();
pp.eat();
}
}
to sum up:
Java实现多态有三个必要条件:继承、重写、向上转型。
继承:在多态中必须存在有继承关系的子类和父类。
重写:子类对父类中某些方法进行重新定义,在调用这些方法时就会调用子类的方法。
向上转型:在多态中需要将子类的引用赋给父类对象,只有这样该引用才能够具备技能调用父类的方法和子类的方法。
只有满足了上述三个条件,我们才能够在同一个继承结构中使用统一的逻辑实现代码处理不同的对象,从而达到执行不同的行为。
对于Java而言,它多态的实现机制遵循一个原则:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法。