Abstract classes in Java

Source: Internet
Author: User

I. What is abstract class

Abstract classes are often used to characterize the abstract concepts of analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. An abstract class is incomplete and can only be used as a base class.

Two. How are abstract classes implemented?

Abstract classes contain abstract methods, abstract methods use the keyword abstract modification, and the abstract method has no method body.

Abstract method:

Abstract void F1 (); // method body cannot appear

Note: Abstract classes can contain non-abstract methods.

Because an abstract class cannot be instantiated, you can use inheritance to abstract the parent class in such a way that the subclass implements all of the parent class's abstract methods , and then instantiates the child class.

The code is as follows:

 Public classAbstractclasstext { Public Static voidMain (string[] args) {A A1=NewB ();//abstract types can use polymorphic//a a2= new A ();//Abstract classes cannot be instantiated. B B1 =NewB ();//subclasses of all methods that implement an abstract class can be instantiated    }}/** Abstract Class A, which contains abstract methods, cannot be instantiated*/Abstract classA {Abstract voidF1 ();//method body cannot appear    Abstract voidF2 (); voidF3 () {; }}classBextendsA {@OverridevoidF1 () {System.out.println ("Class B implements the F1 method of Class A"); } @OverridevoidF2 () {System.out.println ("Class B implements the F2 method of Class A"); }}

Three. Abstract class and Polymorphic

Although abstractions cannot be instantiated, they can be polymorphic.

Abstract classes in Java

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.