Java abstract class

Source: Internet
Author: User

First, abstract class

When you decorate a class with the abstract keyword, this class is called an abstract class, and when you use an abstract class to decorate a method, this method is called an abstract method.

Classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, abstract methods must be overridden

Abstract classes cannot be instantiated

Abstract method Order declaration without prior

Package javastudy.summary;/** * Parent class Animal * with abstract in front of class, this is declared like this: abstract class Animal * This Animal class becomes an abstract class */    Abstract class Animal {public String name;    Public Animal (String name) {this.name = name;     }/** * Abstract method * Here there is only the definition of the method, there is no implementation of the method.     */public abstract void enjoy (); }/** * The subclass cat here inherits from the abstract class animal and naturally inherits the abstract method enjoy (), which is declared within the animal class, * But the subclass cat feels it is not appropriate to implement this enjoy () method, so it declares itself as an abstract class, * then, Who is going to implement this abstract enjoy method, who inherits the subclass, who goes to implement this abstract method enjoy ().    * @author GaCl * */abstract class Cat extends Animal {/** * cat add its own unique properties */public String Eyecolor;    Public Cat (string n, string c) {super (n);//constructor method for calling parent class animal this.eyecolor = C; }}/** * Subclass BlueCat inherits the abstract class cat and implements an abstract method inherited from the parent cat enjoy * @author GaCl * */class BlueCat extends Cat {public BlueCat (Strin    G N, String c) {super (n, c);    }/** * Implements the abstract method enjoy */@Override public void enjoy () {System.out.println ("Blue cat called ..."); }}/** * Subclass dog Inherits abstract class animal, and implements a pumpingLike method enjoy * @author GaCl * */class Dog extends Animal {/** * Dog class to add its own unique properties */public String Furcolor;    Public Dog (string n, string c) {super (n);//Call the parent class animal construction method This.furcolor = C;    } @Override public void enjoy () {System.out.println ("dog called ....");         }}public class Testabstract {/** * @param args */public static void main (string[] args) {/**         * After declaring the cat class as an abstract class, you can no longer instantiate the cat class, because the abstract class is incomplete and lacks arms and legs, so abstract classes cannot be instantiated.        *///cat C = new Cat ("CatName", "Blue");        Dog d = new Dog ("Dogname", "Black");        D.enjoy ();//Call yourself to implement the Enjoy method BlueCat C = new BlueCat ("Bluecatname", "Blue"); C.enjoy ();//Invoke the Enjoy method you have implemented}}

  

Java states that when there is an abstract method in a class, the class must be declared as an abstract class.

When a subclass inherits the parent class, if there is an abstract method in the parent class, and the subclass feels that it can go ahead with all the abstract methods of the parent class, the child class must implement all the abstract methods of the parent class.

If, in the abstract method of the parent class, the subclass is not realized, then the handle class is declared as 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.