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

Example Analysis of object-oriented Polymorphism in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of Java object-oriented polymorphism, which is very detailed and has a certain reference value. Interested friends must read it!

/ * * knowledge point: polymorphic Polymorphism 1. The polymorphism of objects can be divided into three types: overloading of methods, coverage of methods, and upward-downward transformation of objects. The upward and downward transformation of the basic type 3. The upward and downward transformation of the object must occur between the subclass and the parent class Note: 1. Subclass objects can be converted to parent objects, but subclass properties and methods are not visible after conversion. When converted to subclasses, type conversion 2. If you directly instance the parent class and then convert it to a subclass, you are not allowed to report the exception ClassCastException 3. When the subclass overrides the method of the parent class, the method overridden by the subclass is called when the subclass becomes the parent class. * / public class Polymorphism {public static void main (String [] args) {/ / conversion of type byte a = 1; int b = 100; int c = 200; / / small type-> large type b = a; System.out.println (b) / / large types-> small types must be cast and may lose precision a = (byte) c; System.out.println (a) after conversion / / upward and downward transformation of the object / / object upward transformation subclass object-> some properties and methods of the parent object subclass object are not visible / / Classical quotation (subclass is the parent class) the student is human Person p = new Student ("Li Ming"); p.say () / / object downward transformation parent object-> subclass object must be cast / / people can convert to student Student s = (Student) p; s.say (); s.study (); / / Student S2 = (Student) new Person () / / s2.say ();}} class Person {public void say () {System.out.println ("speak");}} class Student extends Person {private String name; public Student (String name) {this.name = name } public void say () {System.out.println ("speak English");} public void study () {System.out.println ("Learning");}} above is all the content of the article "sample Analysis of Java object-oriented Polymorphism". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to 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: 0

*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

Development

Wechat

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

12
Report