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

Detailed explanation of the characteristics of java Polymorphism

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "java polymorphic features detailed explanation", in daily operation, I believe many people have doubts on java polymorphic features detailed explanation, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "java polymorphic features detailed explanation" doubts helpful! Next, please follow the small series to learn together!

What is polymorphism?

Polymorphism is that an object has multiple forms, that is, the reference type variable of the parent class points to the object of the subclass or the reference type variable of the interface points to the object of the interface implementation class. The premise of use must exist inheritance and implementation relations. Simply put, a reference variable points to which class instance object and which class the method called by this reference variable belongs to must be determined during the program execution. In this way, you can change the code bound to the program at runtime without modifying the source code, so that the program has multiple runtime states. This is polymorphism.

polymorphic attention condition

To achieve polymorphism, you need to pay attention to the following prerequisites:

a. There needs to be an inheritance relationship;

b. Subclasses need to override methods of parent classes;

c. You need to assign objects of a subclass to references of a parent class

Member visit characteristics

Member variables: compile to see parent class, run to see parent class

Member methods: compile to see parent class, run to see child class

Static methods: compile to see parent class, run to see parent class

Polymorphism (static binding, dynamic binding)

1. Static binding: that is, the method name is the same, but the method signature is not the same. The argument and which method parameter matches to the maximum extent will call which overloaded method, such as int type argument can not find the corresponding int type parameter overloaded method will look for double type parameter method, if the argument is a reference to the object, will first find the parameter for its own class type, can not find the parameter for the parent class type reference overloaded method. Overloading determines which method to call at compile time and does not vary depending on the specific object to which the reference refers. This code is shown below:

public class A{

void fun(String str);

void fun(int number);

}

2. Dynamic binding: i.e. parent class and child class have the same method name. Parent class references point to child objects and call an overridden method. Before the program runs, it does not know which method to call. It is only known at runtime, because the method of the same name of the object specifically pointed to by the parent class reference will be called at runtime. This is uncertain at compile time, because the compilation passes only because at compile time, the compiler will only look for the method name in the parent class represented by the parent class reference. If it is found, And have permission to call compiled through. This code is shown below:

public class A{

void fun() {

System.out.println("I'am class A.fun()");

}

}

public class B extends A{

@Override

void fun() {

System.out.println("I'am class B.fun()");

}

}

Advantages and disadvantages of polymorphism

Advantages:

1)substitutability: polymorphism has substitutability for existing code;

2)Extensibility: Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, and operation of other features of existing classes. In fact, it is easier to obtain polymorphic functions by adding new subclasses;

3)Interface-ability: polymorphism is a superclass that provides a common interface to subclasses through method signatures, and is implemented by subclasses to complete or override it;

4)Flexibility: It reflects flexible and diverse operations in applications and improves efficiency;

5)Simplicity: polymorphism simplifies the process of writing and modifying code for application software, especially when dealing with calculations and operations on a large number of objects.

Disadvantages:

1)Subclass-specific properties and behaviors cannot be used

summary

At this point, on the "java polymorphism characteristics detailed explanation" of the study is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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