Easy Tutorial
❮ Scala Method Functio Different Android Tutorial Android Studio ❯

In-Depth Understanding of Java Interfaces and Abstract Classes

Category Programming Techniques

Abstraction is one of the key characteristics of object-oriented programming. In Java, the abstraction of OOP can be manifested in two forms: interfaces and abstract classes. These two have many similarities and differences. Many beginners may think they can be used interchangeably, but in reality, they are not. Today, let's learn about interfaces and abstract classes in Java together.


I. Abstract Classes

Before understanding abstract classes, let's first understand abstract methods. An abstract method is a special method: it only has a declaration and no specific implementation. The declaration format for an abstract method is:

abstract void fun();

An abstract method must be modified with the abstract keyword. If a class contains an abstract method, it is called an abstract class, and the class must be modified with the abstract keyword in front of the class. Since an abstract class contains methods without specific implementations, you cannot create objects with an abstract class.

One issue to note is that in the book "Thinking in Java," an abstract class is defined as "a class containing an abstract method." However, it was later found that if a class is only modified with abstract without containing an abstract method, it is also an abstract class. In other words, an abstract class does not necessarily have to contain an abstract method. Personally, I think this is a nitpicking issue, because if an abstract class does not contain any abstract methods, why design it as an abstract class? So for now, just remember this concept and don't delve into why.

[public] abstract class ClassName {
    abstract void fun();
}

From this, it can be seen that abstract classes exist for inheritance. If you define an abstract class and do not inherit it, then it is equivalent to creating the abstract class in vain, because you cannot do anything with it. For a parent class, if implementing a certain method in the parent class makes no sense and must be implemented differently according to the actual needs of the child class, then this method can be declared as an abstract method, and the class becomes an abstract class.

A class containing an abstract method is called an abstract class, but it does not mean that an abstract class can only have abstract methods. Like a regular class, it can also have member variables and regular member methods. Note that there are three main differences between an abstract class and a regular class:


II. Interfaces

An interface, known as interface in English, generally refers to methods or functions that are available for others to call in software engineering. From this, we can appreciate the original intention of the Java language designers, which is the abstraction of behavior. In Java, the form of defining an interface is as follows:

[public] interface InterfaceName {

}

An interface can contain variables and methods. However, it should be noted that variables in an interface are implicitly specified as public static final variables (and can only be public static final variables, using private will result in a compilation error), and methods are implicitly specified as public abstract methods and can only be public abstract methods (using other keywords, such as private, protected, static, final, etc., will result in a compilation error), and all methods in the interface cannot have specific implementations, meaning that all methods in the interface must be abstract methods. From this, we can vaguely see the difference between interfaces and abstract classes. An interface is an extremely abstract type, more "abstract" than an abstract class, and variables are generally not defined in interfaces.

To make a class comply with a specific set of interfaces, the implements keyword is used, with the specific format as follows:

class ClassName implements Interface1,Interface2,[....]{
}

It can be seen that a class is allowed to comply with multiple specific interfaces. If a non-abstract class complies with an interface, it must implement all the methods in that interface. For an abstract class that complies with an interface, it can choose not to implement the abstract methods in the interface.


Differences between Abstract Classes and Interfaces

1. Differences at the Syntactic Level

❮ Scala Method Functio Different Android Tutorial Android Studio ❯