[The Road to becoming a King of Java] Chapter 14: Java SE (Object-Oriented Programming - Composition, Polymorphism)

Objectives of this section

combination

polymorphism

1. What is a combination

Similar to inheritance, composition is also a way to express the relationship between classes, and it can also achieve the effect of code reuse. For example, to represent a school:

public class Student { 
 ... 
} 
public class Teacher { 
 ... 
} 
public class School { 
 public Student[] students; 
 public Teacher[] teachers; 
} 

Composition doesn't involve special syntax (keywords such as extends), it just takes an instance of one class as a field of another class. This is one of the common ways we design classes

Combinatorial representation has - a semantics

In the previous example, we can understand that a school "contains" several students and teachers

Inheritance represents is - a semantics

In the above example of "animals and dogs", we can understand that a dog is also "is" an animal.

Second, what is polymorphism

Polymorphism: Literal understanding: one thing has many forms (this sentence must not be said to the interviewer) 

Upward transformation: 

Upcasting: one sentence, parent class reference, reference subclass object

class Animal{
    public String name;
    public int age;

    public void eat(){
        System.out.println("eat()");
    }

    public Animal(String name,int age){
        this.name = name;
        this.age = age;
    }
}
class Dag extends Animal {
    public Dag(String name,int age){
        super(name,age);
    }


}
class Bird extends Animal{

    public String wing;

    public void fly(){
        System.out.println(age+"fly");

    }

    public Bird(String name,int age,String wing){
        super(name,age);
        this.wing = wing;
    }

}
public class TestDemo{
    public static void main(String[] args) {
        //Dag dag = new Dag("HAHAH",12);
        //Animal animal = dag;
        Animal animal1 = new Dag("HAHAH",12);
    }

What happens when an upcast occurs:

1. Direct assignment

2. Method parameter transfer

3. The method returns

We have demonstrated the method of direct assignment. The other two methods are not substantially different from direct assignment

Method parameters:

 At this time, the type of the formal parameter animal is Animal (base class), which actually corresponds to the instance of Dag (parent class)

 method returns:

Dynamic binding:

Dynamic binding:

Two premises:

1, the parent class reference, refer to the object of the subclass

2. Through this parent class reference, call the override method with the same name of the parent class and the child class

Overriding method of the same name, term: overriding

Rewrite:

1: The method name is the same

2: The parameter list is the same (number + type)

3: The return value is the same

Overriding must be in the case of parent and child classes

Dynamic binding is the basis of polymorphism


// Animal.java 
public class Animal { 
 protected String name; 
 public Animal(String name) { 
 this.name = name; 
 } 
 public void eat(String food) { 
 System.out.println("我是一只小动物"); 
 System.out.println(this.name + "正在吃" + food); 
 } 
} 
// Bird.java 
public class Bird extends Animal { 
 public Bird(String name) { 
 super(name); 
 } 
 public void eat(String food) { 
 System.out.println("我是一只小鸟"); 
 System.out.println(this.name + "正在吃" + food); 
 } 
} 
// Test.java 
public class Test { 
 public static void main(String[] args) { 
 Animal animal1 = new Animal("圆圆"); 
 animal1.eat("谷子"); 
 Animal animal2 = new Bird("扁扁"); 
 animal2.eat("谷子"); 
 } 
} 
// 执行结果
我是一只小动物
圆圆正在吃谷子
我是一只小鸟
扁扁正在吃谷子

At this point, we found:

Although both animal1 and animal2 are references of type Animal, animal1 points to an instance of type Animal, and animal2 points to an instance of type Bird.

Call the eat method for animal1 and animal2 respectively, and find that animal1.eat() actually calls the method of the parent class, while animal2.eat() actually calls the method of the subclass

Therefore, in Java, when a method of a class is called, which piece of code (is it the code of the parent class method or the code of the child class method) is executed, it depends on whether the reference points to the parent class object or the child class object. This process is determined at program runtime (not compile time), so it is called dynamic binding

Precautions:

1. The method cannot be static

2. The access modifier of the subclass must be greater than or equal to the access modifier of the parent class

3. The private method cannot be overridden

4. The method modified by final cannot be overridden

In addition, for overridden methods, you can use the @Override annotation to explicitly specify

// Bird.java 
public class Bird extends Animal { 
 @Override 
 private void eat(String food) { 
 ... 
 } 
} 

Having this annotation can help us perform some legality checks. For example, if the method name is misspelled (such as aet), then the compiler will find that there is no aet method in the parent class, and it will compile and report an error, prompting Cannot constitute overriding. We recommend explicitly adding @Override annotation when overriding methods in code

Summarize:

Let's briefly understand the concept of polymorphism first, and then we will deepen the understanding and understanding of polymorphism through code, so that everyone can fully grasp the grammar knowledge of polymorphism.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324172090&siteId=291194637