Polymorphic Learning continues abstract classes and interfaces

Source: Internet
Author: User

Abstract class

The class with the keyword abstract is called an abstract class, and an abstract class cannot be instantiated, that is, an object (instance) of an abstract class that cannot be new.

Abstract methods

The method that is modified with the abstract keyword is called an abstract method .

Abstract methods must be defined in an abstract class.

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

The corresponding method of declaring an implementation can be called a concrete method .

The relationship between abstract classes and abstract methods

Abstract methods must be defined in the abstract class.

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

If a class is an abstract class, the class can contain either abstract methods or concrete methods (declarations, implementations).

Abstract classes are also allowed if they are all concrete methods, and abstract classes can be empty, meaning nothing.

Inheritance of abstract classes

In the case where the parent class is an abstract class, there are two choices when the subclass inherits from the parent class:

1. Subclasses are abstract classes

The subclass still needs the abstract keyword when declaring it, and subclasses can choose to implement or not implement the abstract method of the parent class (because the abstract class can also include concrete methods, even all of which can be concrete methods).

However, because a subclass is an abstract class, it cannot be instantiated.

2. Subclasses are not abstract classes

Subclasses can be instantiated when they are not abstract, but at this time the subclass must implement all the abstract methods of the parent class.

You do not need to use the abstract keyword when implementing an abstraction method.

Use of abstract classes

An abstract method of an abstract class defines a specification, or a convention, that is given to subclasses for implementation.

Because the implementation of an abstract class may not be complete or meaningless.

As an example:

Define an abstract class shape, and then triangle,circle and rectangle such as inheritance Shape,shape define an abstract method to calculate the area, and then implement this method in each subclass to calculate the respective area.

At this point, if you do not use abstract classes and abstract methods, that is, the shape class is a normal class, you can also do this function, that is, by using the subclass method to overwrite the parent class method.

But at this point the parent class, the method in shape, is to provide a concrete implementation, first of all do not know how to calculate the area of the abstract shape, if the parent area defines a constant, such as 0 or 1, it is not clear meaning.

Interface

The interface is declared with the keyword interface .

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

interface in the definition of the method, you can use the abstract keyword, you can omit the abstract keyword, (most of the time is omitted), the method is still abstract, cannot have the implementation of curly braces.

Interfaces and abstract classes function Similarly, interfaces cannot be instantiated, and interfaces can be thought of as a special abstract class (all abstract methods).

The polymorphic usage of an interface is similar to an abstract class, and a reference to an interface type can point to an object of the class that implements the interface.

The differences between interfaces and abstract classes are as follows:

The methods in the interface must be all abstract methods, and the methods in the abstract class can be abstract or concrete.

A class can implement an interface, using the keyword implements. Java is single-inheritance, but it can implement multiple interfaces. (a class can inherit another class at the same time and implement multiple interfaces.) )

if a class implements an interface, and the class is not an abstract class, then the class must implement all the methods in this interface. in the case of an abstract class, you do not need to implement all the methods in the interface.

Polymorphic

Polymorphism is a reference to a parent type that can point to an object of a subtype, or a reference to an interface type can point to an instance of the class that implements the interface .

The forced type conversion between the interface and the class that implements the interface is exactly the same as the coercion type conversion between the parent class and the child class. (See the Polymorphic articles link below).

Articles related to this blog

Interface: http://www.cnblogs.com/mengdd/archive/2012/08/22/2650309.html

Polymorphic: http://www.cnblogs.com/mengdd/archive/2012/12/25/2832288.html

Polymorphic Learning continues abstract classes and interfaces

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.