Javase personal garbage review notes 11 Java Override and Overload and Polymorphism

Method rewriting rules The
parameter list and the parameter list of the rewritten method must be exactly the same.

The return type and the return type of the overridden method can be different, but must be a derived class of the return value of the parent class (the return type of java5 and earlier versions must be the same, and the return type of java7 and later versions can be different).

The access authority cannot be lower than the access authority of the overridden method in the parent class. For example: if a method of the parent class is declared as public, then overriding the method in the child class cannot be declared as protected.

The member methods of the parent class can only be overridden by its subclasses.

A method declared as final cannot be overridden.

A method declared as static cannot be overridden, but it can be declared again.

If the subclass and the superclass are in the same package, the subclass can override all methods of the superclass, except for the methods declared as private and final.

The subclass and the parent class are not in the same package, so the subclass can only override the non-final methods of the parent class declared as public and protected.

The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a broader mandatory exception than the one declared by the overridden method, and vice versa.

The construction method cannot be overridden.

If you cannot inherit a method, you cannot override this method.

class Animal{
    
    
   public void move(){
    
    
      System.out.println("动物可以移动");
   }
}
 
class Dog extends Animal{
    
    
   public void move(){
    
    
      System.out.println("狗可以跑和走");
   }
   public void bark(){
    
    
      System.out.println("狗可以吠叫");
   }
}
 
public class TestDog{
    
    
   public static void main(String args[]){
    
    
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象
 
      a.move();// 执行 Animal 类的方法
      b.move();//执行 Dog 类的方法
      b.bark();
   }
}
/*以上实例编译运行结果如下:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

Use of the Super keyword
When you need to call the overridden method of the parent class in the subclass, use the super keyword.

class Animal{
    
    
   public void move(){
    
    
      System.out.println("动物可以移动");
   }
}
 
class Dog extends Animal{
    
    
   public void move(){
    
    
      super.move(); // 应用super类的方法
      System.out.println("狗可以跑和走");
   }
}
 
public class TestDog{
    
    
   public static void main(String args[]){
    
    
 
      Animal b = new Dog(); // Dog 对象
      b.move(); //执行 Dog类的方法
 
   }
}
/*以上实例编译运行结果如下:

动物可以移动
狗可以跑和走

Overloading Overloading
is in a class with the same method name but different parameters. The return type can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

The most common place is the overloading of the constructor.

Reload rules:

The overloaded method must change the parameter list (the number of parameters or types are different); the
overloaded method can change the return type; the
overloaded method can change the access modifier; the
overloaded method can declare new or Check exceptions more broadly;
methods can be overloaded in the same class or in a subclass.
The return value type cannot be used as the criterion for distinguishing overloaded functions.

Insert picture description here

Insert picture description here
Insert picture description here
Java polymorphism

Polymorphic use cases

class Person
{
    
    
   public void fun1()
   {
    
    
      System.out.println("1.Person{fun1()}") ;
   }
   public void fun2()
   {
    
    
      System.out.println("2.Person{fun2()}") ;
   }
}

// Student类扩展自Person类,也就继承了Person类中的fun1()、fun2()方法
class Student extends Person
{
    
    
   // 在这里覆写了Person类中的fun1()方法
   public void fun1()
   {
    
    
      System.out.println("3.Student{fun1()}") ;
   }
   public void fun3()
   {
    
    
      System.out.println("4.Studen{fun3()}") ;
   }
}

class TestJavaDemo1
{
    
    
   public static void main(String[] args) 
   {
    
    
      // 此处,父类对象由子类实例化
      Person p = new Student() ;
      // 调用fun1()方法,观察此处调用的是哪个类里的fun1()方法
      p.fun1() ;
      p.fun2() ;
   }
}
//运行结果
/*
3.Student{fun1()}
2.Person{fun2()}
  1. Upward transition
public class Animal {
    
    
    public void eat(){
    
    
        System.out.println("animal eatting...");
    }
}

public class Cat extends Animal{
    
    

    public void eat(){
    
    

        System.out.println("我吃鱼");
    }
}

public class Dog extends Animal{
    
    

    public void eat(){
    
    

        System.out.println("我吃骨头");
    }

    public void run(){
    
    
        System.out.println("我会跑");
    }
}

public class Main {
    
    

    public static void main(String[] args) {
    
    

        Animal animal = new Cat(); //向上转型
        animal.eat();

        animal = new Dog();
        animal.eat();
    }

}

//结果:
//我吃鱼
//我吃骨头

This is the upward transformation, Animal animal = new Cat(); transforms the child object Cat into the parent object Animal. At this time, the method called by the animal reference is a subclass method.

  1. Downcast
class A {
    
    
         public void print() {
    
    
                  System.out.println("A:print");
         }
}

class B extends A {
    
    
         public void print() {
    
            
                  System.out.println("B:print");
         }
}

public class Test{
    
    
         public static void main(String args[])
         {
    
    
                  A a = new B();          //通过子类去实例化父类
                  a.print();
         }
}
//B:print

Downcasting is to turn the parent object into a child object.


Animal a = new Cat();
Cat c = ((Cat) a);
c.eat();
//输出  我吃鱼
Dog d = ((Dog) a);
d.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Cat cannot be cast to com.chengfan.animal.Dog
Animal a1 = new Animal();
Cat c1 = ((Cat) a1);
c1.eat();
// 报错 : java.lang.ClassCastException:com.chengfan.animal.Animal cannot be cast to com.chengfan.animal.Cat

Why doesn't the first code report an error? Compared to what you already know, because a itself is a Cat object, it can of course be transformed into a Cat, and of course it cannot be transformed into a Dog. Have you ever seen the fucking phenomenon of a dog suddenly becoming a cat?

While a1 is an Animal object, it cannot be downcast to any subclass object. For example, if you go to archaeology and discover a new creature, you know that it is an animal, but you can't directly say, ah, it is a cat, or that it is a dog.

class X {
    
    
    public void show(Y y){
    
    
        System.out.println("x and y");
    }

    public void show(){
    
    
        System.out.println("only x");
    }
}

class Y extends X {
    
    
    public void show(Y y){
    
    
        System.out.println("y and y");
    }
    public void show(int i){
    
    

    }
}

class main{
    
    
    public static void main(String[] args) {
    
    
        X x = new Y();
        x.show(new Y());
        x.show();
    }
}
//结果
//y and y
//only x

Y inherits X and overrides the show(Y y) method in X, but does not override the show() method.

At this time, the object pointed to by x with a reference type of X is Y. At this time, the method to be called is determined by Y and will be searched from Y first. Execute x.show(new Y());, the method is defined in Y, so the method in Y is executed;

But when x.show(); is executed, some people will say that there is no such method in Y? It seems to find the method in the parent class, because the method in X is called.

In fact, there is a show() method in class Y. This method inherits from X, but it does not cover this method, so it is not written explicitly in Y. It looks like a method in X is called. In fact The call is still in Y.

//Hahaha I don’t want to live anymore

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108586987