Java abstract class

Source: Internet
Author: User
Tags define abstract

Abstract class animal{

???? Public abstractvoid eat ();

}

class Dog extends animal{

???? Public void eat () {

???????? System. out. println (" The dog eats bones . ");

????}

}

class Cat extends animal{

???? Public void eat () {

???????? System. out. println (" The cat eats the mouse ");

????}

}

class Wolf extends animal{

???? Public void eat () {

???????? System. out. println (" Eat Pleasant goat ");

????}

}

class Test02 {

???? Public staticvoid main (String args[]) {

???????? Animal aa[]=new animal[3];

????????

???????? aa[0]=New Dog ();

???????? aa[1]=New Cat ();

???????? aa[2]=New Wolf ();

???????? For (int i=0;i<3;i++)

???????????? {aa[i].eat ();}

????}

}

/*

Experimental results:

The dog eats bones.

The cat eats the mouse

Eat Pleasant goat

?

Abstract class: A class that can only be inherited as a parent class and cannot be instantiated

?

Why do you need abstract classes?

???? In some cases not all classes can fully describe the behavior of the class, only know what methods should be included,

???? but without knowing what to do, you can define abstract classes without having to implement their methods.

?

Abstract classes can contain abstract and non-abstract methods,

It is also possible to not include abstract and non-abstract methods.

as long as there is an abstract method, it is an abstract class. ????

?????

????

*/

Java abstract class

Related Article

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.