In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand the polymorphism in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
I. the concept of polymorphism
Inheritance relationships enable a subclass to inherit the characteristics of the parent class and attach some new features. A subclass is the specialization of its parent class, and the instance of each subclass is an instance of its parent class, but the reverse is not true. For example, each circle is a geometric object, but not every geometric object is a circle. Therefore, you can always pass an instance of a subclass to a parameter that requires a parent type. The reference examples are as follows:
Public class PolymorphismDemo {/ * * Main method * / public static void main (String [] args) {displayObject (new Circle (1, "red", false)); displayObject (new Rectangle (1 black, true));} public static void displayObject (GeometriicObject object) {System.out.println ("Created on" + object.getDateCreated () + ".Color is" + object.getColor ());}}
Created on Mon Mar 09 19:25:20 EDT 2011.Color is red
Created on Mon Mar 09 19:25:20 EDT 2011.Color is black
The method displayObject has parameters of type GeometriicObject. You can call displayObject by passing any instance of GeometriicObject. Objects of subclasses can be used wherever the parent class object is used. This is commonly referred to as polymorphism. Simply put, polymorphism means that variables of the parent type can refer to objects of the subtype.
Second, the characteristics of polymorphism
The premise of polymorphism: inheritance (that is, there must be a child parent relationship. When a method is called using a polymorphic parent class reference variable, the overridden method of the subclass is called Definition format: parent class type variable name = new subclass type (); parent class reference points to subclass objects, such as Animal a = new Cat (); in polymorphisms, compile looks to the left and runs to the right
3. Instanceof operator
Instanceof is the keyword for Java. Every letter in the Java keyword is lowercase.
In order to better understand the type conversion, it can be considered that they are similar to the relationship among animals, polar bears and pandas, in which animal Animal is the parent class of polar bear Polar bear and panda Panda. Polar bears are animals, so you can always safely assign an instance of Polar bear to the Animal variable. The keyword is used to determine whether an object belongs to a data type, and the return value is of Boolean type.
Fu Zz=new Xu (); Fu Zzz=new yiting (); if (F1 instanceof Xu) {System.out.println ("Zz is the type of Xu");} else {System.out.println ("Zzz is the type of yiting");} IV. Polymorphic transformation 1. Upward transformation.
You can always convert an instance of a subclass to a variable of a parent class, called up conversion, because an instance of a subclass is always an instance of its parent class.
Function:
Reduce some repetitive code when instantiating objects, you can instantiate different objects according to different requirements
Package project2; class Animal {int num=100; void say () {System.out.println ("It's an Animal.");} class Cat extends Animal {int num=50 Void say () {System.out.println ("It's a Cat.");} void bark () {System.out.println ("Meow!") }} public class project2 {public static void main (String [] args) {Animal cat=new Cat (); / / upward transition System.out.println (cat.num); cat.say (); / / cat.bark () }}
Running result:
one hundred
It's a Cat.
Upward transformation do not force the transformation, the method that the parent class reference points to or calls is the method of the subclass, this is called dynamic binding. After the upward transformation, the parent class reference cannot call the subclass's own methods.
2. Downward transformation
Converts an instance of a parent class to its subclass variable. You must use the conversion tag (subclass name) to display the conversion to indicate your intention to the compiler. For the conversion to be successful, you must ensure that the object you want to convert is an instance of a subclass.
Function:
When you make an upward transition, you lose other methods that are unique to the subclass object; you can go back through the downward transition.
Package project2;class Animal {int num=100; void say () {System.out.println ("It's an Animal.");}} class Cat extends Animal {int num=50; void say () {System.out.println ("It's a Cat.") } void bark () {System.out.println ("Meow!");}} public class project2 {public static void main (String [] args) {Animal cat=new Cat (); / / upward transition Cat cat2= (Cat) cat;// to downward transformation System.out.println (cat2.num) Cat2.say (); cat2.bark ();}}
Running result:
fifty
It's a Cat.
Meow, Meow, Meow!
5. Method rewriting
Tip: to override a method, you need to define the method in the subclass with the same signature as the parent class.
The subclass inherits methods from the parent class. Sometimes, subclasses need to modify the implementation of methods defined in the parent class, which is called method rewriting.
The following points are worth noting:
The overridden method must have the same signature as the overridden method and the same or compatible return type. Compatibility means that the return type of an overridden method can be a subtype of the return type of the overridden method. It can be overridden only if the instance method is accessible. If the methods defined in the subclass are private in the parent class, the two methods are completely unrelated. Like instance methods, static methods can be inherited. However, static methods cannot be overridden. If the static method defined by the parent class is redefined in the subclass, the static method defined in the parent class will be hidden. You can use the syntax parent class name. static method name calls hidden static methods. Prevent inheritance and rewriting
Neither a class nor a method modified by final can be inherited. The data field modified by final is a constant.
Sometimes you may want to prevent classes from being inherited. In this case, use the final modifier to indicate that a class is the final class and cannot be used as a parent class. The Math class is a final class. The String, StringBuilder, and StringBuffer classes, as well as wrapper classes for all basic data types, are also final. For example, the following class An is the final class and cannot be inherited:
Public final class A {/ / Data fields,constructors, and methods omitted}
You can also define a method as final, and the final method cannot be overridden by its subclasses. For example, the following cannot be rewritten:
Public class Test {/ / Data fields,constructors, and methods omitted public final void m () {/ / Do something}}
Note: the modifiers public, protected, private, static, abstract, and final can be used on classes and members of classes, and only the final modifier can be used on local variables in methods. The final local variable in the method is a constant.
In order to override a method, a method in a subclass must be defined with the same signature or the same or compatible return type as the method in its parent class. Instance methods can be overridden only if they are accessible. In this way, a private method cannot be overridden because it cannot be accessed outside the class itself. If the methods defined in the subclass are private in the parent class, then the two methods are completely unrelated! Static methods can be inherited as well as instance methods. However, static methods cannot be overridden, and if static methods defined in the parent class are redefined in the subclass, the methods defined in the parent class are hidden. You can use the expression obj instanceof AClass to test whether an object is an instance of a class. You can use the final modifier to indicate that a class is final and cannot be inherited, or that a method is final and cannot be overridden.
After reading the above, do you have any further understanding of how to understand polymorphism in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.