Java Object-oriented polymorphism

Source: Internet
Author: User

Java object-oriented polymorphism

Polymorphism Performance in Java:

1, overloading and rewriting of methods (overwrite);

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:

1  Package Com.java1234.chap03.sec13; 2  3  Public class Animal {4  5      Public void say () {6         System.out.println ("I am an animal"); 7     }8 }

Two more subcategories, the dog and cat classes, overriding the parent class method say:

 1  package   com.java1234.chap03.sec13;  2  3  public  class  Cat extends   animal{ 4  5  public  void   say () {  6  System.out.println ("I Am a cat"  7  }  8  }
1  Package Com.java1234.chap03.sec13; 2  3  Public class extends animal{4  5      Public void say () {6         System.out.println ("I Am a Dog"); 7     }8 }

Then we write a test class:

1  PackageCom.java1234.chap03.sec13;2  3  Public classTest {4  5      Public Static voidMain (string[] args) {6Dog dog=NewDog ();7 Dog.say ();8          9Cat cat=NewCat ();Ten Cat.say (); One     } A}

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:

1  PackageCom.java1234.chap03.sec13;2  3  Public classTest {4  5      Public Static voidMain (string[] args) {6         //Parent class reference to the specific implementation of the dog subclass7Animal animal=NewDog ();8 Animal.say ();9          Ten         //Replacement Implementation OneAnimal=NewCat (); A 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:

1  PackageCom.java1234.chap03.sec13;2  3  Public classTest {4  5      Public Static voidMain (string[] args) {6         //Parent class reference to the specific implementation of the dog subclass7Animal animal=NewDog ();8 Animal.say ();9          Ten         //Down Transformation OneDog dog=(Dog) animal; A Dog.say (); -           -         //down transformation because animal points to the specific implementation of dog, it will error theCat cat=(Cat) animal; - Cat.say (); -     } -}

Operation Error:

I'm a dog.

I'm a dog.

Exception in thread "main" Java.lang.ClassCastException:com.java1234.chap03.sec13.Dog cannot is cast to Com.java1234.chap03.sec13.Cat

At Com.java1234.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:

1  Package Com.java1234.chap03.sec13; 2  3  Public Interface people {4  5      Public void say (); 6 }

Next write two specific implementation classes, Student Teacher:

  1  package   com.java1234.chap03.sec13;   2   3  public  class  Student implements   people{  4   5   @Override   6  public   void   Say () {  7  System.out.println (" I am a student "  8  }   9  10 } 
  1  package   com.java1234.chap03.sec13;   2   3  public  class  Teacher implements   people{  4   5   @Override   6  public   void   Say () {  7  System.out.println (" I am the teacher "  8  }   9  10 } 

Let's write a test class:

1  PackageCom.java1234.chap03.sec13;2  3  Public classTest2 {4  5      Public Static voidMain (string[] args) {6People p1=NewStudent ();7 P1.say ();8          9p1=NewTeacher ();Ten P1.say (); One     } A}

Run output:

I am a student

I'm a teacher.

Java Object-oriented polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.