Java abstract class and abstract method (abstract)

Source: Internet
Author: User

When certain methods of the parent class are not deterministic, the abstract keyword can be used to decorate the method [abstraction], and abstract to decorate the class [abstract class].

We all know that the parent class is to extract the properties and methods that the subclass has in common, some of which have already been explicitly implemented, some of which cannot be determined, then we can define them as abstractions and reuse them in the later days to materialize. In this way, the abstract class is born.

For example, the "animal" parent class is defined, where the "animal name" and "Animal age" properties have been clarified, but the "animal called" method is not clear, and "animal call" can be defined as an abstract method.

  Therefore, abstract classes are designed to extract the same but indeterminate things for later reuse. The purpose of defining abstract classes is to implement abstract classes in subclasses.

Package Javastudy;public class AbstractDemo1 {public    static void Main (string[] args) {        //TODO auto-generated met Hod stub    }}//This is an abstract class of abstraction classes Animal {    String name;    int age;    Animals will be called public    abstract void Cry ();//Not sure how animals are called. Defined as an abstract method to resolve the uncertainty of the parent class method. Abstract methods cannot be implemented in the parent class, so there is no body of the function. However, in subsequent inheritance, this method is implemented specifically. }//abstract classes can be inherited//When the inherited parent class is an abstract class, all the abstract methods in the abstract class need to be fully implemented. Class Cat extends Animal {    //Implements Cry abstract method of the parent class public    void Cry () {        System.out.println ("cat called:");}    }

  

The class that is modified with abstract, that is, abstract, is modified by the abstract method, that is, abstraction. An abstract method cannot have a method body, inheriting an abstract parent class from a subclass, overriding the abstract method of the parent class.

Abstract class can not be instantiated, because the method in the abstract class is not materialized, is an incomplete class, the direct instantiation of no meaning.

Abstract classes do not necessarily contain abstract methods, and abstract classes can have no abstract methods.

Once the class contains the abstract method, the change class must be an abstract class.

Java abstract class and abstract method (abstract)

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.