Abstract class

1. Abstract class Overview

In Java, a method that has no method body is defined as an abstract method, and a class that has abstract methods must be defined as an abstract class.

2. Characteristics of abstract classes

  • Abstract classes and abstract methods must be decorated with the abstract keyword

Public abstract class class name {}

Public abstract void method name ()

  • An abstract class does not necessarily have abstract methods, but a class with abstract methods must be an abstract class.
  • Abstract classes can only be instantiated by subclass objects in a polymorphic manner. This is called abstract class polymorphism
  • A subclass of an abstract class can either override an abstract method in an abstract class or be an abstract class.

3. Membership characteristics of abstract classes

  • Member variables

A member variable can be a variable or a constant.

  • A constructor

Abstract classes have constructors, but cannot be instantiated. Constructors are primarily used to initialize subclasses’ access to data from their parent classes.

  • Members of the method

You can have abstract methods that qualify subclasses to perform certain actions

There is also a non-abstract approach: improve code reuse

2. Interface

1. Overview of interfaces

The interface is a common specification standard, as long as it meets the specification standard, everyone can use it. Interfaces in Java are more about abstraction of behavior

2. Interface features

  • The interface is decorated with the keyword interface

Public interface Interface name {}

  • Class implementation interfaces are represented by implements

Public class name implements interface name {}

  • Instantiation of the interface

Interfaces cannot be instantiated directly. They can only be instantiated in a polymorphic way by implementing class objects. This is called interface polymorphism.

  • The implementation class of the interface

Like subclasses of abstract classes, the implementing class of an interface either overrides all the abstract methods in the interface or is an abstract class.

3. Interface member features

  • Member variables

Must be constant and has the default modifier: public static fianal

  • A constructor

There are no constructors for interfaces, because interfaces are primarily abstractions of behavior and do not exist concretely. A class that has no parent class inherits from object by default.

  • Members of the method

A member method in an interface can only be abstract because it has a default modifier: public Abstract

The difference between abstract classes and interfaces

1. Grammatical differences

  • Members of the difference between
Members of the difference between
An abstract class Variable, constant; There are construction methods; There are abstract methods and non-abstract methods
interface Constants, abstract methods
  • Relationship between the difference
Relationship between the difference
Class with the class Inheritance, single inheritance
Classes and interfaces Implementation, can be single implementation, can also be more implementation
Interfaces and interfaces Inheritance, single inheritance, multiple inheritance

2. Differences in design concepts

Interfaces are designed to “constrain” the behavior of classes, that is, to provide a mechanism to force different classes to behave the same way. Interfaces only restrict behavior, not how it can be implemented, so they mostly abstract behavior. Abstract classes are designed for code reuse. Abstract classes are formed by analyzing objects and refining their internal commonalities to represent the essence of objects. Therefore, abstract classes are abstractions of classes, including their attributes and behaviors. As a result, developers have different motivations for using them. Developers inherit abstract classes in order to use their properties and behavior, and developers implement interfaces only in order to use their behavior.

In short, abstract classes are abstractions of things, while interfaces are abstractions of behavior.