Home  >  Article  >  Java  >  What are the manifestations of java polymorphism?

What are the manifestations of java polymorphism?

WBOY
WBOYforward
2023-05-11 19:37:131816browse

1. Overriding

During the inheritance process, a subclass defines a method with the same name, the same parameters, and the same return value as the parent class, which is called overriding

When rewriting, the subclass cannot have more restrictive access permissions than the parent class

Benefits of rewriting: Increase the flexibility of the code

Person p1 = new Student();
   Person p2 = new Teacher();
   p1.work(); //p1会调用Student类中重写的work方法
   p2.work(); //p2会调用Teacher类中重写的work方法

2. Overloading

In the same class, defining multiple methods with the same name and different parameters is called overloading, which has nothing to do with the return value.

Different parameters are represented by different numbers, types, and orders

The benefits of overloading: increasing the flexibility of the code

3. Abstraction Class

In the Java language, a method in a class gives a standard without giving a specific implementation method. Such a class is an abstract class.

abstract class Fu {
     public abstract void method();
         }
class Zi extends Fu {
public void method(){
     System.out.println(“重写父类抽象方法”);
}
}
//类的多态使用
Fu fu= new Zi();

4. Interface

In the polymorphic mechanism, interfaces are more convenient to use than abstract classes, and the collection of abstract classes is the interface.

abstract class Fu {
     public abstract void method();
         }
class Zi extends Fu {
public void method(){
     System.out.println(“重写父类抽象方法”);
}
}
//类的多态使用
Fu fu= new Zi();

The above is the detailed content of What are the manifestations of java polymorphism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete