Pet pet=new Dog (); Pet is the parent class, and dog is a subclass. This method is the parent class that does the type, and the subclass is instantiated.
Related examples:
Examples of employees returning home;
1, first to do an abstract parent class, including the idea of walking.
Package Cn.day1;public Abstract class Traffic {public abstract void run ();
2, in creating a class that contains the idea of going home.
Package Cn.day1;public class Employee {public void gohome (traffic tool) {Tool.run ();}}
3, create two subclasses, inherit the parent class. Subclass contains two tools to go home.
Package Cn.day1;public class Tube extends traffic {@Overridepublic void run () {System.out.println ("Tube Run");}}
Package Cn.day1;public class Car extends traffic {@Overridepublic void run () {System.out.println ("Car Run");}}
4, call
Package Cn.day1;public class MyText {public static void main (string[] args) {//Staff employee Ele=new employee ();//Transport Traffi C tra=new Car (); Ele.gohome (TRA);}}
I learn not solid, later in-depth study I will optimize the content of this chapter!!!
The method of the parent class as the parameter list in the Java polymorphism