Dark Horse programmer-abstract class and interface, dark horse programmer Abstract

Source: Internet
Author: User

Dark Horse programmer-abstract class and interface, dark horse programmer Abstract

 

I recently studied the Java SE video tutorial by Miss Bi Xiangdong. This is the background.

This article describes the simple concepts of abstract classes and interfaces.

Abstract class

KeywordAbstractThe modified class is called an abstract class. An abstract class cannot be instantiated, that is, an object (Instance) of an abstract class cannot be created ).

Abstract Method

The method modified with the abstract keyword is calledAbstract Method.

Abstract methods must be defined in abstract classes.

Abstract methods include declarations and implementations (no curly braces {}, and curly braces, but empty content is also an implementation ).

The corresponding declarative implementation method can be calledMethod.

Relationship between abstract classes and abstract methods

Abstract methods must be defined in abstract classes.

If a class contains abstract methods, the class must be declared as an abstract class.

If a class is an abstract class, this class can contain both abstract methods and specific methods (with declarations and implementations ).

In the abstract class, ifAllThe specific method is also allowed; the abstract class can also be empty, that is, nothing is included.

Inheritance of abstract classes

When the parent class is an abstract class and the Child class inherits the parent class, there are two options:

1. Subclass is an abstract class

The abstract keyword is still required for the subclass declaration. The subclass can implement or not implement the abstract method of the parent class (because the abstract class can also include specific methods, or even all specific methods ).

However, a subclass or an abstract class cannot be instantiated.

2. Subclass is not an abstract class

The subclass can be instantiated when it is not an abstract class, but it must implement all abstract methods of the parent class.

Abstract keywords are no longer required to implement abstract methods.

Usage of abstract classes

An abstract method of an abstract class defines a specification or convention. The specific implementation is handed over to the subclass.

This is because the implementation of abstract classes may not be completed or meaningless.

For example:

Define an abstract class Shape, and then Triangle, Circle, and Rectangle inherit Shape. Shape defines an abstract method to calculate the area, and then implement this method in each subclass to calculate the area.

At this time, if you do not need abstract classes or abstract methods, that is, the Shape class is a common class, you can also complete this function by overwriting the parent class method with the subclass method.

But at this time, the parent class, that is, the method in the Shape, must provide specific implementation. First, we do not know how to calculate the area of this abstract Shape. If the parent class area defines a constant, such as 0 or 1, the meaning is unclear.

Interface

Keyword USED FOR THE INTERFACEInterfaceStatement.

The interface is equivalent to class, and all methods in the interface are abstract methods.

When defining a method in an interface, you can use the abstract keyword or omit the abstract keyword (which is omitted most of the time). The method is still abstract and cannot contain curly braces.

Similar to abstract classes, interfaces cannot be instantiated and can be considered as special abstract classes (all abstract methods ).

The method of multi-state interface usage is similar to the abstract class. The reference of the interface type can point to the object of the class that implements this interface.

The differences between interfaces and abstract classes are as follows:

All methods in the interface must be abstract methods. Methods in the abstract class can be abstract or specific.

Class can implement the interface,Keyword implements. Java is a single inheritance, but multiple interfaces can be implemented.(A class can inherit from another class at the same time and implement multiple interfaces .)

If a class implements an interface and this class is not an abstract class, this class must implement all the methods in this interface.If it is an abstract class, you do not need to implement all methods in the interface.

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.