Java section 36th Abstract class overview and use

Source: Internet
Author: User
Tags stub

2016-06-29

1 Definition of abstract class

Abstract classes provide a specification for subclasses.

Modifier abstract class Name {
Class Body
Modifier abstract Returns a value type method name (parameter list);
}

There is no method body for abstract methods: Public abstract void Test ();
The common method has the method body: public void Test () {//code in the method body};

1, define a class, if this class is an abstract class, then there is at least one abstract method in this class.
2, abstract class method is not necessarily an abstract method, but the meaning of the abstract method of the class must be an abstract class.

 Packagecom.java1995;/*** Abstract class: Cat class *@authorAdministrator **/ Public Abstract classFeline {/*** Package*/    Private intlegcount=4;//define the number of legs    PrivateString headshape= "Round";//define the shape of the head    Private BooleanMeateater=true;//defines whether it is a carnivore,        /*** Abstract Method: Predation *@paramAnimal*/     Public Abstract voidcatchanimal (Animal Animal); /*** Common method: Yell*/     Public voidcry () {System.out.println ("Animals in Howling"); }        /*** Implementation of the package *@return     */     Public intGetlegcount () {returnLegcount; }     Public voidSetlegcount (intLegcount) {         This. Legcount =Legcount; }     PublicString Getheadshape () {returnHeadshape; }     Public voidSetheadshape (String headshape) { This. Headshape =Headshape; }     Public BooleanIsmeateater () {returnMeateater; }     Public voidSetmeateater (Booleanmeateater) {         This. Meateater =Meateater; }}

2 use of abstract classes

 Package com.java1995; /**  @author*/Publicclass  Animal {}
 Package com.java1995; /**  @author   */public class extends Animal {}

 Packagecom.java1995;/*** Subclass of abstract class: Tiger *@authorAdministrator **/ Public classTigerextendsfeline{/*** Rewrite abstract method: Tiger's Prey*/@Override Public voidcatchanimal (Animal Animal) {//TODO auto-generated Method StubSystem.out.println ("The Tiger is hunting"); }         Public Static voidMain (string[] args) {Mouse Mouse=NewMouse (); Tiger T=NewTiger ();    T.catchanimal (mouse); }}

 Packagecom.java1995;/*** Subclass of abstract class: Cat *@authorAdministrator **/ Public classCatextendsfeline{/*** Rewrite abstract method: Cat's Prey*/@Override Public voidcatchanimal (Animal Animal) {//TODO auto-generated Method Stub//The first thing to judge is whether the animal is a mouse, and if it is, it can hunt.        if(AnimalinstanceofMouse) {System.out.println ("The cat is hunting mice."); }Else{System.out.println ("The cat can't hunt it."); }    }         Public Static voidMain (string[] args) {Mouse Mouse=NewMouse (); Cat Cat=NewCat ();    Cat.catchanimal (mouse); }}

@Override used to detect whether the rewrite succeeded.
1. If a class inherits an abstract class, it must override all the abstract methods of the abstract class.
2. If there is a class that does not have an abstract method to rewrite the abstract class, the class is also defined as an abstract class.

 Packagecom.java1995;/*** Abstract Method class *@authorAdministrator **/ Public Abstract classTest {/*** Abstract Method*/     Public Abstract voidtest1 ();  Public Abstract voidtest2 ();}
 Packagecom.java1995;/*** Subclass of Abstract Method class *@authorAdministrator **/ Public classSubtestextendstest{/*** A class inherits an abstract class, it must override all the abstract methods of the abstract class * @Override to detect whether the rewrite succeeded. */@Override Public voidtest1 () {//TODO auto-generated Method StubSystem.out.println ("Method 1"); } @Override Public voidtest2 () {//TODO auto-generated Method StubSystem.out.println ("Method 2"); }}
 Package com.java1995; /**  @author*/publicabstractclass  AbstractTest1 {      Public Abstract void test ();          Public void test2 () {        System.out.println ("This is the normal method");}    }

Resources

[1] Java Easy Start Classic tutorial "full version"

Java section 36th Abstract class overview and use

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.