In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the use of Java polymorphism". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the use of Java polymorphism"?
One. Explore the upward transformation (upcasting)
The act of treating an object reference as a "reference to base type" is called an upward transition.
1. When a function is called after Upcasting, if the function is overwritten in derived class, the function in derived class will be called; otherwise, the function in base class will be called. Such as
Class First {
Public void prt () {
System.out.println ("First")
}
}
Class Second extends First {
/ / (a)
Public void prt () {
System.out.println ("Second")
}
}
Public class ExplicitStatic {
Public static void main (String [] args) {
First n = new Second ()
N.prt ()
}
}
The result is Second. If the prt () function in Second class is commented out, First will be output.
2. After the upward transformation, you can only call the functions in base class that are overridden by derived class.
/ *
Abstract class First {
Int I = 122,
Public void prt () {
System.out.println ("First.i =" + I)
}
Public abstract void prt (First f)
}
XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >
Class Second extends First {
Public void prt () {
System.out.println ("Second.i =" + I)
}
Public void prt (First I)
{
}
Public void prt (int I)
{
}
}
Public class ExplicitStatic {
Public static void main (String [] args) {
First n = new Second ()
N.prt (2)
}
}
, /
Class First {
Public void prt () {
System.out.println ("First")
}
}
Class Second extends First {
/ / (a)
Public void prt () {
System.out.println ("Second")
}
Public void prt (int I) {/ / (a)
System.out.println ("Second.i =" + I)
}
}
Public class ExplicitStatic {
Public static void main (String [] args) {
First n = new Second ()
N.prt (3)
}
}
The function at (a) is only a function in Second class, so it cannot be called through n.prt (3).
II. Abstract class and Abstract methods
1. If an abstract class exists in a class, the class must also be declared as abstract class.
2. Abstract class cannot be instantiated.
3. If base class is an abstract class, then derived class must implement all abstract methods; in base class. Otherwise, derived class must also be declared as abstract class.
III. Other key points
1. Pure inheritance and expansion
Pure inheritance: only functions suggested by base class are overridden by derived class.
Expansion: in addition to overriding base class's functions, it also implements its own functions.
Abstract class First {
Public abstract void f ()
Public abstract void g ()
}
/ / Pure inheritance
Class Second extends First {
Public void f () {}
Public void g () {}
}
/ / expansion
Class Third extends First {
Public void f () {}
Public void g () {}
Functions that do not exist in public void u () {} / / base class
}
2. Transition downwards
1) when transforming downwards, you can only call functions that have been overridden in base class.
2) only if it is already a derived class object can you correctly turn it down.
Class First {
Public void f () {}
Public void g () {}
}
Class Second extends First {
Public void f () {}
Public void g () {}
Public void u () {}
Public void v () {}
}
Public class ExplicitStatic {
Public static void main (String [] args) {
First [] x = {new First (), new Second ()}
X [0] .f ()
X [1] .g ()
/ /! X [1] .u (); there is no function u () in class First
/ ((Second) x [0]) .f (); (a)
((Second) x [1]) .u ()
}
}
(a) A ClassCastException exception is thrown at.
At this point, I believe you have a deeper understanding of "what is the use of Java polymorphism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.