W Sub-class name ();
3.2 Method Name (subclass object);
The return type of the 3.3 method is the parent class
Access modifier parent class name Method name ([parameter list]) {
Return parent class object or subclass object;
}
//Subclass, Round Public classCircular extends Graph {PrivateFinalDoublex=3.14;//radius PublicCircular (DoubleR) {super (R); } Public voidGetarea () {DoubleM= (Super.getlen () *x*y); System. out. println ("the radius of the circle is"+super.getlen () +"\ n area of"+m); }
//sub-class, rectangular Public classRectangle extends Graph {Private DoubleWeight//width PublicRectangle (DoubleLenDoubleweight) {super (LEN); This. weight=weight; } Public Doublegetweight () {returnweight; } Public voidGetarea () {DoubleM= (Super.getlen () * This. Getweight ()); System. out. println ("the length of the rectangle is"+super.getlen () +", wide for"+ This. weight+"\ n area of"+m); }
//Parent Class Public Abstract classGraph {Private DoubleLen//side length of graphs PublicGraph (DoubleLen) { This. len=Len; } //Get length Public DoubleGetlen () {returnLen; } //abstract Approach-oriented Public Abstract voidGetarea ();}
4. Conversion between polymorphic Neutron class and parent class
a) conversion of subclasses to parent class (up conversion)
Parent class Object name = new subclass ();//Automatic conversion
The name of the object. Method name ([value]);
Use rule "P72" for upward conversion:
The parent class refers to a subclass object when referred to as up conversion or transformation (upcasting), and is automatically type converted
L refer to a variable through the parent class, called by a subclass overriding (overriding) or inheriting the parent class method "Choice 1, 2"
L reference variable through parent class, cannot call subclass-specific method "Select Question 3"
b) Conversion of the parent class to a subclass (down conversion)--coercion of type conversions, restoring the true face of the object (which seed class is specific)
Parent Class object name = new Subclass ();
Object name. Subclass method ();//Compile Error
should be modified to:
Parent class Object name = new Subclass ();
Subclass sub-Class object = (subclass) object name;
Subclass object. Subclass Method ();
//Parent Class Public Abstract classZhu {PrivateString name;//the names of the two PrivateString Food; PrivateString Nation; /** * @return the name*/ PublicString GetName () {returnname; } PublicString getnation () {returnNation; } PublicZhu (String name,string food,string Nation) { This. name=name; This. food=Food ; This. nation=Nation; } PublicString Getfood () {returnFood ; } Public Abstract voidprint ();} Parent class
//sub-class Public classWang extends zhu{PrivateString habit; PublicWang (String name,string habit,string food,string Nation) {super (name,food,nation); This. habit=habit; } PublicString gethabit () {returnhabit; } Public voidprint () {System. out. println ("My name"+super.getname () +", from"+super.getnation () +", like to eat"+ This. Getfood () +", Hobby practice"+ This. Habit); }} Subclass
a) conversion of subclasses to parent class (up conversion)
Parent class Object name = new subclass ();//Automatic conversion
The name of the object. Method name ([value]);
Use rule "P72" for upward conversion:
The parent class refers to a subclass object when referred to as up conversion or transformation (upcasting), and is automatically type converted
L refer to a variable through the parent class, called by a subclass overriding (overriding) or inheriting the parent class method "Choice 1, 2"
L reference variable through parent class, cannot call subclass-specific method "Select Question 3"
b) Conversion of the parent class to a subclass (down conversion)--coercion of type conversions, restoring the true face of the object (which seed class is specific)
Parent Class object name = new Subclass ();
Object name. Subclass method ();//Compile Error
should be modified to:
Parent class Object name = new Subclass ();
Subclass sub-Class object = (subclass) object name;
Subclass object. Subclass Method ();
Parent Classsub-class
//sub-class Public classYuehanextendszhu{PrivateString habit; PublicYuehan (String name,string habit,string food,string Nation) {Super(name,food,nation); This. habit=habit; } PublicString gethabit () {returnhabit; } Public voidprint () {System.out.println ("My Name" +Super. GetName () + ", from" +Super. Getnation () + ", like to eat" + This. Getfood () + ", hobby is playing" + This. Gethabit ()); }}
Java object-oriented chapter III