Java Object-oriented polymorphism
Polymorphism Performance in Java:
1, overloading and rewriting of methods;
2, the parent class can be used to point to the specific implementation of subclasses, and can be changed at any time to other subclasses of the specific implementation;
Let's start with a parent class animal:
Package com.htli.chap03.sec13; public class Animal {public void say () { System.out.println ("I am an Animal");} }
Two more subcategories, the dog and cat classes, overriding the parent class method say:
Package com.htli.chap03.sec13; public class Cat extends animal{public void Say () { System.out.println ("I Am a Cat"); }
Package com.htli.chap03.sec13; public class Dog extends animal{public void Say () { System.out.println ("I am a Dog");} }
Then we write a test class:
Package com.htli.chap03.sec13; public class Test {public static void Main (string[] args) { dog dog=new dog (); Dog.say (); Cat cat=new Cat (); Cat.say (); }}
Run output:
I'm a dog.
I am a cat
I'm sure you can see this. Simple subclass inheritance and instantiation of object invocation methods;
Let me use the polymorphic test, the parent class reference to the subclass concrete implementation:
Package com.htli.chap03.sec13; public class Test {public static void Main (string[] args) { //parent class reference to the specific implementation of the dog subclass Animal animal=new Dog ();
animal.say (); Replace the implementation of animal=new Cat (); Animal.say (); }}
Run output:
I'm a dog.
I am a cat
Transformation of objects:
Transition up: Child Object---Parent class object security
Down Transformation: Child class object, parent object, not secure
Example above: Animal animal=new Dog (); It is the new Dog () subclass object that is later Animail animal transformation is safe;
A downward transition is a parent class object that is cast to a subclass object: Let's change the test class:
Package com.htli.chap03.sec13; public class Test {public static void Main (string[] args) { //parent class reference to the specific implementation of the dog subclass Animal animal=new Dog ();
animal.say (); Downward transformation of dog dog= (dog) animal; Dog.say (); Down transformation because animal points to the specific implementation of dog, it will error the cat cat= (cat) animal; Cat.say (); }}
Operation Error:
I'm a dog.
I'm a dog.
Exception in thread "main" Java.lang.ClassCastException:com.htli.chap03.sec13.Dog cannot is cast to Com.htli.chap03.sec13.Cat
At Com.htli.chap03.sec13.Test.main (test.java:15)
Conversion exception so the downward transformation is not safe, we must know the concrete implementation class;
The inheritance method we used above demonstrates that polymorphism is actually more of an interface in actual development:
Let's write one more example:
Let's start with a parent class interface people:
Package com.htli.chap03.sec13; Public interface People {public void Say ();}
Next write two specific implementation classes, Student Teacher:
Package com.htli.chap03.sec13; public class Student implements people{ @Override public Void Say () { System.out.println ("I am a student"); }
Package com.htli.chap03.sec13; public class Teacher implements people{ @Override public Void Say () { System.out.println ("I am a Teacher"); }
Let's write a test class:
Package com.htli.chap03.sec13; public class Test2 {public static void Main (string[] args) { people p1=new Student (); P1.say (); P1=new Teacher (); P1.say (); }}
Run output:
I am a student
I'm a teacher.
Java Object-oriented polymorphism