Java interface and abstract class instance analysis

This article describes the java interface and abstract classes. Share with you for your reference. The specific analysis is as follows:

Abstract is one of the characteristics of object-oriented programming. In Java, OOP abstraction can be embodied in two forms: interface and abstract class. There are too many similarities and too many differences between the two. Many people think they can be used interchangeably at first, but they are not. Today, let's learn about interfaces and abstract classes in Java.

If there is anything wrong, please understand and welcome criticism and correction. I'm not very grateful.

I abstract class

Before understanding abstract classes, let's take a look at abstract methods. Abstract method is a special method: it has only declaration, but no concrete implementation. The declaration format of the abstract method is:

abstract void fun();

Abstract methods must be decorated with the abstract keyword. If a class contains abstract methods, it is called an abstract class. The abstract class must be decorated with the abstract keyword before the class. Because abstract classes contain methods without concrete implementation, objects cannot be created with abstract classes.

Here's a problem to pay attention to: in the Book Java programming ideas, abstract classes are defined as "classes containing abstract methods", but later it is found that if a class does not contain abstract methods and is only modified with abstract, it is also an abstract class. That is, abstract classes do not necessarily contain abstract methods. Personally, I think this is a question of cutting corners, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So let's remember this concept for the time being. There's no need to delve into why.

It can be seen from here that abstract classes exist for inheritance. If you define an abstract class but don't inherit it, you create this abstract class for nothing, because you can't do anything with it. For a parent class, if a method implemented in the parent class has no meaning and must be implemented differently according to the actual needs of the child class, the method can be declared as an abstract method, and the class will become an abstract class at this time.

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

1) The abstract method must be public or protected (because if it is private, it cannot be inherited by subclasses, and subclasses cannot implement the method). By default, it is 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 method of the parent class, the subclass must also be defined as an abstract class.

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

II Interface

Interface is called interface in English. In software engineering, interface generally refers to methods or functions called by others. From here, we can understand the original intention of the Java language designer, which is the abstraction of behavior. In Java, the form of an interface is as follows:

Interfaces can contain variables and methods. But be careful, Variables in the interface will be implicitly specified as public static final variables (and can only be public static final variables, and private modification will report compilation errors), while methods will be implicitly specified as public abstract methods and can only be public abstract methods (modification with other keywords, such as private, protected, static, final, etc., will report compilation errors), and all methods in the interface cannot have specific implementation, that is, the methods in the interface must be abstract methods. From here, we can vaguely see the difference between the interface and the image class. The interface is an extremely abstract type, which is more "abstract" than the abstract class And generally, variables are not defined in the interface.

To make a class follow a set of special interfaces, you need to use the implements keyword. The specific format is as follows:

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

III The difference between abstract classes and interfaces

1. Grammatical differences

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

2) Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type;

3) Interfaces cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

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

2. Differences at the design level

1) Abstract class is the abstraction of a thing, that is, the abstraction of class, while interface is the abstraction of behavior. Abstract class is to abstract the whole class, including attributes and behaviors, But the interface is local to the class (behavior) abstract. For a simple example, aircraft and birds are different kinds of things, but they all have one thing in common, that is, they can fly. In design, aircraft can be designed as an airplane and birds as a bird, but flight can not be designed as a class, so it is only a behavior feature, not a class Abstract description of things. At this time, the flight can be designed as an interface fly, including the method fly (), and then aircraft and bird implement the interface fly according to their own needs. Then, as for different types of aircraft, such as fighter aircraft and civil aircraft, they can directly inherit airplane, which is similar to birds. Different types of birds can directly inherit bird. It can be seen from here that inheritance is a yes / no relationship, while interface implementation is a yes / no relationship. If a class inherits an abstract class, the subclass must be the type of the abstract class, and the interface implementation has a relationship with or without, such as whether a bird can fly (or whether it has the characteristics of flying). If it can fly, it can implement the interface, and if it can't fly, it won't implement the interface.

2) Different from the design level, abstract class, as the parent of many subclasses, is a template design. The interface is a code of conduct, which is a radial design. What is template design? The simplest example is that everyone has used the templates in PPT. If ppt B and PPT C are designed with template a, the common parts of PPT B and PPT C are template A. if their common parts need to be changed, only template a needs to be changed. There is no need to change ppt B and ppt C again. The radial design, such as an elevator, is equipped with some kind of alarm. Once the alarm is to be updated, it must be completely updated. That is to say, if you need to add a new method to an abstract class, you can directly add a specific implementation to the abstract class without changing the subclass; But not for the interface. If the interface is changed, all classes that implement the interface must be changed accordingly.

Let's take a look at one of the most popular examples on the Internet: doors and alarms: doors have two actions: open () and close (). At this time, we can define this abstract concept through abstract classes and interfaces:

Or:

But now, if we need the door to have the function of alarm (), how to implement it? Here are two ideas:

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

2) Put these three functions in the interface. Classes that need to use the alarm function need to implement the open () and close () functions in the interface. Maybe this class does not have the open () and close () functions at all, such as fire alarm.

It can be seen from here that door's open (), close () and alarm () basically belong to the behaviors in two different categories. Open () and close () belong to the inherent behavior characteristics of the door itself, while alarm () belongs to the extended additional behavior. Therefore, the best solution is to design the alarm as a separate interface, including the alarm () behavior, and the door as a separate abstract class, including open and close behaviors. Another alarm door is designed to inherit the door class and implement the alarm interface.

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>