Java interface and abstract class instance analysis

  • 2020-04-01 03:53:26
  • OfStack

This article illustrates Java's interfaces and abstract classes. Share with you for your reference. Specific analysis is as follows:

Abstraction is one of the hallmarks of object-oriented programming. In Java, OOP abstractions can be represented in two forms: interfaces and abstract classes. There are too many similarities and too many differences between the two. Many people think they can be used interchangeably when they are just starting out, but they can't. Today we're going to look at interfaces and abstract classes in Java.

If there is anything wrong, please understand and welcome criticism, not very grateful.

I. abstract class

Before you look at abstract classes, look at abstract methods. An abstract method is a special kind of method: it has only declarations and no concrete implementation. The declaration format of the abstract method is:

The abstract void fun ();

Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, the class is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Abstract classes cannot be used to create objects because they contain methods that have no concrete implementation.

Here's a catch: in the book JAVA programming ideas, abstract classes are defined as "classes that contain abstract methods," but it turns out that if a class doesn't contain abstract methods, it's also an abstract class. That is, an abstract class does not have to contain abstract methods. Personally, this is a tough question, because if an abstract class doesn't contain any abstract methods, why design it as an abstract class? So keep that in mind for the moment, and don't get into why.


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

As you can see, abstract classes exist for inheritance. If you define an abstract class and don't inherit from it, you create the abstract class for nothing, because you can't do anything with it. For a parent class, if one of its methods does not make any sense to be implemented in the parent class and must be implemented differently depending on the actual needs of the subclass, then the method can be declared as the abstract method, and the class becomes the abstract class.

Classes that contain abstract methods are called abstract classes, but that does not mean that there can only be abstract methods in the abstract class, which, like ordinary classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) the abstract method must be public or protected (because if it is private, it cannot be inherited by subclasses, which cannot implement the method), and by default it defaults to public.

2) abstract classes cannot be used to create objects;

3) if a class inherits from an abstract class, the subclass must implement the abstract methods of the parent class. If the subclass does not implement the abstract methods of the parent class, then the subclass must also be defined as the abstract class.

In other respects, abstract classes are no different from ordinary classes.

2. Interface

In software engineering, an interface is a method or function that someone else calls. From here, we can appreciate that the designers of the Java language intended it to be an abstraction of behavior. In Java, the form of an interface is as follows:


[public] interface InterfaceName {
 
}

Interfaces can contain variables and methods. However, it should be noted that variables in the interface are implicitly specified as public static final variables (and can only be public static final variables; private modifiers will cause compilation errors), while methods are implicitly specified as public abstract methods and can only be public abstract methods (using other keywords, such as private, protected, static, final modifiers will cause compilation errors), And all methods in the interface cannot have a concrete implementation, that is, the methods in the interface must all be abstract methods. The difference between an interface and an abstract class is a subtle one. An interface is an extremely abstract type, more "abstract" than an abstract class, and generally does not define variables in an interface.

To make a class conform to a particular set of interfaces, you need to use the implements keyword, in the following format:


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

As you can see, allowing a class to follow multiple specific interfaces. If a non-abstract class follows an interface, it must implement all the methods in that interface. For an abstract class that follows an interface, you may not implement abstract methods in that interface.

The distinction between abstract class and interface

1. Differences in grammar

1) abstract classes can provide the implementation details of member methods, while public abstract methods can only exist in the interface;

2) the member variables in the abstract class can be of various types, while the member variables in the interface can only be of public static final type;

3) the interface cannot contain static code blocks and static methods, while the abstract class can have static code blocks and static methods;

4) a class can only inherit from one abstract class, while a class can implement multiple interfaces.

2. Differences at the design level

1) an abstract class is an abstraction of a thing, that is, of a class, while an interface is an abstraction of a behavior. Abstract classes abstract the whole class, including properties and behaviors, but interfaces abstract the parts of the class. For example, airplanes and birds are different things, but they have one thing in common: they can fly. So in design, you can design an Airplane as an Airplane, a Bird as a Bird, but you can't design a flight as a class, so it's just a behavioral feature, not an abstract description of a class of things. At this point, you can design the flight as an interface, Fly, with the method Fly (), and then Airplane and Bird implement the Fly interface according to their own needs. And then there are different kinds of planes, like fighter planes, civil planes, you can directly inherit from Airplane, and it's the same for birds, you can directly inherit from Bird for different kinds of birds. As you can see here, inheritance is a "yes" relationship, whereas interface implementations are "no" relationships. If a class inherits an abstract class, then the subclass must be the type of the abstract class, and the interface implementation is a relationship with or without, such as whether a bird can fly (or whether it has the characteristic of flying), the interface can be implemented if it can fly, but not if it cannot fly.

2) the design level is different. Abstract class, as the parent of many subclasses, is a kind of template design. And an interface is a behavior specification, it's a kind of radial design. What is template design? The simplest example is that everyone has used PPT template. If PPT B and PPT C are designed with template A, the common part of PPT B and PPT C is template A. If the common part of PPT B and PPT C needs to be changed, only template A needs to be changed. Radiative design, such as an elevator equipped with some kind of alarm, once to update the alarm, it must be all updated. That is to say, for the abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, the subclass can not change; Not so with interfaces. If the interface changes, all the classes that implement the interface must change accordingly.

Here's one of the most popular examples on the Internet: the example of a door and an alarm: a door both has two actions, open() and close(), so we can define the abstract concept through abstract classes and interfaces:


abstract class Door {
  public abstract void open();
  public abstract void close();
}

Or:


interface Door {
  public abstract void open();
  public abstract void close();
}

But now if we want the door to have an alarm() function, how do we do that? Here are two ideas:

1) put the three functions in the abstract class, but in this way all the subclasses inherited from the abstract class have the alarm function, but some doors do not necessarily have the alarm function;

2) put these three functions in the interface, the class that needs to use the alarm function needs to implement the open() and close() in the interface, maybe this class does not have the functions of open() and close(), such as the fire alarm.

From this, it can be seen that the open(), close() and alarm() of Door belong to two different categories of behavior at all. Open () and close() belong to the inherent behavior characteristics of the Door itself, while alarm() belongs to the additional behavior of extension. So the best solution is to design the alarm as a separate interface with the alarm() behavior, and the Door as a separate abstract class with the open and close behaviors. Design an Alarm Door again to inherit the Door class and implement the Alarm interface.


interface Alram {
  void alarm();
}
abstract class Door {
  void open();
  void close();
}
class AlarmDoor extends Door implements Alarm {
  void oepn() {
   //....
  }
  void close() {
   //....
  }
  void alarm() {
   //....
  }
}

I hope this article has been helpful to your Java programming.


Related articles: