Java polymorphism 2

Source: Internet
Author: User

Http://www.cnblogs.com/wqq0402/p/6180685.html

Package test05;


public class Duotai_test02 {
/** multiple objects, one pattern
* Tiger, Lion, Snake→animal
*, multiple objects, a form (type).
* Different objects, showing the same pattern after.
* The same functionality can be achieved.
*
* In this way, multiple sub-classes are required to correspond to a parent class.
* It is often implemented by inheriting the parent class with (multiple) subclasses.
*
* @param args
*/

public static void Main (string[] args) {
Animal Animal = new Cat ();//abstract reference to child class object
Animal.eat ();
Animal.sheep ();
Animal animal2 = new Dog ();
Animal2.eat ();
Animal2.sheep ();
Animal animal3 = new Snack ();
Animal3.eat ();
Animal3.sheep ();

duotai_test02 dt = new duotai_test02 ();
Dt.test (New Cat ());
Dt.test (New Dog ());
Dt.test (New Snack ());
}

public void Test (Animal animals) {
Animals.eat ();
Animals.sheep ();

}
}

Abstract class Animal {//Abstraction classes
String name;
int age;
public abstract void Eat ();//abstract method
public abstract void sheep ();//abstract method

}

Class Cat extends Animal {
public void Eat () {//method override
System.out.println ("Cat Eats");
}
public void sheep () {
System.out.println ("Cat Sleeps");
}
}

Class Dog extends Animal {
public void Eat () {//method override
System.out.println ("Dog Eats");
}
public void sheep () {
System.out.println ("The Dog Sleeps");
}
}

Class Snack extends Animal {
public void Eat () {//method override
System.out.println ("Snake Eats");
}

public void sheep () {//method override
System.out.println ("Sleeping Snakes");
}
}

Java polymorphism 2

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.