The abstract class of Java

Source: Internet
Author: User
Tags scream

1, when using the abstract keyword to decorate a class, this class is called abstract class; When you use abstract to modify a method, the method is called an abstract method.

2. Classes containing abstract methods must be declared abstract classes, abstract classes must be inherited, and abstract methods must be rewritten.

3. Abstract classes cannot be instantiated.

4, abstract method only need to declare, and do not need to implement.

 Public classtestpolymorphic{ Public Static voidMain (String args[]) {//Animal Animal = new Animal ("Animal"); Abstract classes cannot be instantiatedCat cat =NewCat ("Cat","Blue"); Dog Dog=NewDog ("Dog","Black"); //Lily L1 = new Lily ("L1", animal);Lily L2 =NewLily ("L2", dog); //L1.myanimalenjoy ();L2.myanimalenjoy (); }}classlily{PrivateString name; PrivateAnimal Animal;  PublicLily (String name, Animal Animal) { This. Name =name;  This. Animal =Animal; }     Public voidMyanimalenjoy () {Animal.enjoy (); }}Abstract classanimal{//declaring abstract Classes    PrivateString name;  PublicAnimal (String name) { This. Name =name; }    /*Public void Enjoy () {System.out.println ("Scream ~ ~ ~"); }*/     Public Abstract voidEnjoy ();//declaring abstract methods}classDog extends animal{PrivateString Forlorcolor;  PublicDog (String name,string forlorcolor) {super (name);  This. Forlorcolor =Forlorcolor; }     Public voidenjoy () {System. out. println ("Dog Barking ~ ~ ~"); }}classCat extends animal{PrivateString Eyescolor;  PublicCat (String name,string eyescolor) {super (name);  This. Eyescolor =Eyescolor; }     Public voidenjoy () {System. out. println ("Cat Scream ~ ~ ~"); }}

Operation Result:

Summary: Abstract classes are generally used in polymorphism.

The abstract class of Java

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.