Abstract classes and Interfaces in Java

Source: Internet
Author: User

Abstract class:

A class that is decorated with the keyword abstract (anywhere before the class keyword) 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.

For keyword abstract----> optional modifiers (static, final, abstract) can coexist, but abstract cannot coexist with static or final (understanding its true meaning)

Abstract method:

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).

Abstract class and abstract method relationships:

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 (but not generally); Abstract classes can be empty, that is, nothing.

Abstract classes have the same constructor, because the method of constructing an abstract class might need to be implemented in a subclass

Example: Public abstract class Shape {
private int base;
Public Shape () {

}
public Shape (int base) {
Super ();
This.base = base;
}

}

public class Rectangle extends Shape {
private int width;
private int height;
public Rectangle (int width, int height) {
Super (4);
This.width = width;
This.height = height;
}

}

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:

acts as a parent class, forcing subclasses to implement the overriding method correctly. Cases:

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, will provide a concrete implementation, but do not know how to calculate the area of this abstract shape.

Interface (also a reference data type----> compensate for the reduced richness of Java due to single inheritance)

The interface is declared with the keyword interface.

The interface is the same as class, and all methods in the interface are public abstract methods (JDK1.8 before). The attributes in the interface must be public, static, constant (three keywords can be omitted).

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).

Interface: Enables classes that do not inherit relationships to share behavior.

Interface implementation: 1, Class implementation interface (using the keyword implements) (1) class is abstract class: Can not implement its methods, but in the subclass is not an abstract class must be fully implemented

(2) A class is not an abstract class: Implements all the methods defined in the interface.

2, (1) classes can implement multiple interfaces: public class Car implements runable,serviceable{

}

(2) interfaces can inherit interfaces, and can inherit multiple: public interface stopable extends runable,serviceable{

}

When using interfaces: (1) Use interfaces to resolve multiple inheritance, (2) Use interfaces to add functionality to external classes.

The differences between interfaces and abstract classes are as follows:

Interface

Abstract class

Defined

Keyword: interface

Keywords:abstract

Public, static, constant properties

Any property

Only public, abstract methods

Any method

There are constructors

There are initialization blocks

Use

class implements interface using keyword implements(can be implemented more)

Interfaces can inherit interfaces (multiple inheritance possible)

class inherits abstract class (single inheritance, using keyword: extends)

Abstract classes can inherit abstract classes (single inheritance)

Design

Ancillary added features (try to design small interfaces)

The innate behavior

Abstract classes and Interfaces in Java

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.