Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the benefits of using java polymorphism

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "what are the benefits of using java polymorphism", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "what are the benefits of using java polymorphism?"

Benefits

1. The cost of using the class by the class caller is further reduced. Polymorphism only needs to know that the object has a method.

Encapsulation is so that the caller of the class does not need to know the implementation details of the class.

Polymorphism allows the caller of a class not to know what the type of the class is, but to know that the object has a method.

2. It is more scalable, and if you want to add a new shape, the cost of code modification using polymorphism is relatively low.

For the caller of the class (the drawShapes method), all you have to do is create an instance of the new class, and the cost of modification is very low.

Example

Class Cycle {private String name = "Cycle"; public static void travel (Cycle c) {System.out.println ("Cycle.ride ()" + c);} public String toString () {return this.name;}} class Unicycle extends Cycle {private String name = "Unicycle"; public String toString () {return this.name;}} class Bicycle extends Cycle {private String name = "Bicycle"; public String toString () {return this.name;}} class Tricycle extends Cycle {private String name = "Tricycle"; public String toString () {return this.name }} public class Demo1 {public static void ride (Cycle c) {c.travel (c);} public static void main (String [] args) {Unicycle unicycle = new Unicycle (); Bicycle bicycle = new Bicycle (); Tricycle tricycle = new Tricycle (); ride (unicycle); ride (bicycle); ride (tricycle) }} the above is the content of this article on "what are the benefits of using java polymorphism". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 273

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report