Home > Back-end >  S2 - Java polymorphism
S2 - Java polymorphism

Time:10-23

a polymorphism.
form 1) parent do parameter, subclasses do arguments
Public void feed (parent object name) {
The object name. The method name ();
}
Instantiation of the class object name. Feed (subclass object name)

form 2.) the parent class do a reference type, a subclass instantiated (upcasting automatic type conversion)
Superclass object name=new subclass name ();

form 3.) the parent class as a method return value, subclass instantiation automatic type conversion)
2. Rewrite the method
1) location: parents and children have the same name method
2). The same method name, return type is the same as the modifier, different method body


3. Instanceof function and use the
Subclasses conversion parent class: upcasting automatic conversion
Parent class conversion subclasses: instanceof (cast downward transition)


:
Polymorphism can decrease the class code, can improve the scalability and maintainability of the code

Upcasting a - subclass into the parent class
Automatic type conversion
Transformation of a parent class down into subclasses
Combining the instanceof operator casts

/*

* code plate*/

/*
* parent class Pet
* */

public class Pet {
Public void eat () {
System. The out. Println (" Pet are eating ");
}
}


/*
* subclass Dog
* */

public class Dog extends Pet {

Public void eat () {
System. The out. Println (" the dog is eat dog food ");
}


//owners and pets play behavior
public void catchdisk () {
System. The out. Println (" the dog dog play frisbee ");
}
}


/*
* subclass Penugin
* */

public class Penugin extends Pet {
Public void eat () {
System. The out. Println (" penguins are eating fish ");
}


//owners and pets play behavior
public void swiming () {
System. The out. Println (" penguin swimming in ");
}
}



/*
* Master class Master
* */

public class Master {

//polymorphic form 1: parent do parameter, subclasses do arguments
public void feed (Pet Pet) { //receive the parent class as a parameter with a subclass argument
pet. Eat ();
}


//pet
//polymorphic form 3: the parent class as a method return value, subclass instantiated (type automatic conversion)

public Pet getpet (int id) {
Pet Pet=null;
If (id==1) {
Pet=new Dog ();
} else if (id==2) {
Pet=new Penugin ();
}
The return of pet;
}


//owners and pets play behavior
public void play (Pet Pet) {
If (pet instanceof Dog) {
//if the superclass object is Dog class will convert
Dog Dog=(Dog) pet; //down transition
t catchdisk ();
{} else if (pet instanceof Penugin)
//if the superclass object is Penugin class will convert
Penugin Penugin=(Penugin) pet;
Penugin. Swiming ();
}
}

}



import Java. Util. Scanner;
/*
* Test class Test
* */

public class Test {
Public static void main (String [] args) {

//polymorphic form 1: parent do parameter, subclasses do arguments
Pet Pet=new Pet ();
Dog dog=new Dog();
Penugin Penugin=new Penugin ();

Master ms=new Master ();
Ms. Feed (pet);
//Penugin as actual parameters passed
Ms. Feed (penugin); //Penugin as actual parameters passed
Ms. Feed (dog); //the Dog as actual parameters passed

//polymorphic form 2: reference type subclasses instantiated parent do
Pet pet2=new Dog (); //parent class conversion subclasses (upcasting automatic conversion)
pet2. Eat ();

//pet method//polymorphic form 3: the parent class as a method return value, subclass instantiated (type automatic conversion)
System. Out. Println (" please select you want to adopt a pet: dog 2. Penguin ");
Scanner input=new Scanner(System.in);
Int the cw=input. NextInt ();
Master dx=new Master ();
Pet pets=dx. Getpet (the cw);
Pets. Eat ();

Dx. Play (dog);
Dx. Play (penugin);

}
}
  • Related