Home > Java > Java Tutorial > body text

What are the manifestations of java polymorphism?

WBOY
Release: 2023-05-11 19:37:13
forward
1714 people have browsed it

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方法
Copy after login

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();
Copy after login
Copy after login

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();
Copy after login
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!