Java abstract class and abstract method (abstract)

Source: Internet
Author: User

I. Use of abstractions (abstract)

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.

 PackageJavastudy; Public classAbstractDemo1 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub    }}//This is an abstract classAbstract classAnimal {String name; intAge ; //animals will call     Public Abstract voidCry ();//Not sure what the animal 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. classCatextendsAnimal {//implementing the Cry abstract method of the parent class     Public voidcry () {System.out.println ("The Cat called:"); }}

The class that is modified with abstract, that is, abstract, is modified by the abstract method, that is, abstraction.

Abstract methods cannot have method bodies. The format is as follows:

Abstract void xxx ();

Abstract classes cannot be instantiated. Because the methods in the abstract class are not materialized, this is an incomplete class, so direct instantiation is meaningless.

The abstract class does not necessarily include the Abstrace method. That is, there can be no abstract method in abstraction.

Vi. once the class contains an abstract method, that class must be declared as 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.