Chapter 2 Polymorphism
One. Reproduction to Upcasting
The action of an Object Reference as a "Reference to Base Type" called upward transformation.
1. When Upcasting is called, if the DeriveD Class overrides the function, the function in the DeriveD Class is called; otherwise, the function in the 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) {
FigSt n = new second ();
n.prt () ;;
}
}
The result is second. If the PRT () function in the Second Class is commented away, the first first will be output.
2. After the upward transition, you can only call the function over the BASE Class in the base class.
/ *
Abstract class first {
INT i = 122;
Public void prt () {
System.out.println ("first.i =" i);
}
Public Abstract Void PRT (First F);
}
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) {
FigSt 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) {
FigSt n = new second ();
N.PRT (3);
}
}
(A) The function at the SECOND Class is only the function in the second class, so it cannot be called via N.PRT (3).
two. Abstract Class and Abstract Methods
1. If ABSTRACT CLASS exists in a class, the class must also be declared as Abstract Class.
2. Abstract Class cannot be instantiated. 3. If the base class is an Abstract Class, Derived Class must implement all Abstract Methods in the base class; otherwise, Derived Class must also be declared as Abstract Class.
three. Other points
1. Pure inheritance and expansion
Pure inheritance: Only the function suggested by the base class is overwritten by Derived Class.
Expansion: In addition to overwriting the Base Class function, you have also implemented your own function.
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 () {}
Public void u () {} // base class does not exist
}
2. Downward transition
1) You can only call the function over the base class when you transfers.
2) You can only turn down when you do it when you use the DeriveD Class object.
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 (); no function u () in Class first
//(Second )X [0] ).f(); (a)
(Second) x [1]). U ();
}
}
(A) Those will throw the ClassCastException exception.