In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about how to understand the Java interface and abstract class principles. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
This article mainly introduces the detailed explanation of Java interface and abstract class principle, which is introduced in great detail through the sample code, which has a certain reference and learning value for everyone's study or work. Friends who need it can refer to it.
For object-oriented programming, abstraction is one of its major features. In Java, the abstraction of OOP can be represented in two forms: interfaces and abstract classes. There are too many similarities and differences between the two. Many people think that they can be used interchangeably when they are beginners, but in fact they are not. Today let's take a look at the interfaces and abstract classes in Java. The following is the catalog outline of this article:
one。 Abstract class
Before you look at abstract classes, let's take a look at abstract methods. An abstract method is a special method: it has only declarations and no concrete implementation. The declaration format of an 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, and the abstract class must be prefixed with the abstract keyword. Because abstract classes contain methods that have no concrete implementation, you cannot create objects with abstract classes.
The next thing to note is to define an abstract class as "a class that contains abstract methods", but later found that if a class does not contain abstract methods, it is also an abstract class if it is decorated with abstract. That is, an abstract class does not have to contain abstract methods. Personally, I think this is a thorny problem, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So keep this concept in mind for a while, and you don't have to dig into why.
[public] abstract class ClassName {abstract void fun ();}
It can be seen here that abstract classes exist for inheritance. If you define an abstract class but don't inherit 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 is implemented in the parent class without any meaning and must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and the class becomes the abstract class.
A class that contains 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 the abstract class and the ordinary class mainly have three-point area [public] abstract class ClassName {abstract void fun ();} if private, it cannot be inherited by the subclass, and the subclass 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 method 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.
two。 Interface
Interface is called interface in English. In software engineering, interface generally refers to methods or functions that can be called by others. From here, we can understand the original intention of Java language designers, which is an abstraction of behavior. In Java, the form of an interface is as follows:
[public] interface InterfaceName {}
Interfaces can contain variables and methods. Note, however, that variables in the interface are implicitly specified as public static final variables (and can only be public static final variables, while methods are implicitly specified as public abstract methods and can only be public abstract methods (with other keywords, such as private, protected, static, final, etc.), and all methods in the interface cannot have a specific implementation, that is, The methods in the interface must all be abstract. You can vaguely see the difference between an interface and an abstract class. An interface is an extremely abstract type, which is more "abstract" than an abstract class, and generally does not define variables in the interface.
For a class to follow a specific set of interfaces, you need to use the implements keyword, in the following format:
Class ClassName implements Interface1,Interface2, [....] {}
As you can see, a class is allowed to follow multiple specific interfaces. If a non-abstract class follows an interface, you must implement all the methods in that interface. For abstract classes that follow an interface, you don't have to implement abstract methods in that interface.
three。 The difference between abstract classes and interfaces
1. Grammatical differences
1) Abstract classes can provide implementation details of member methods, but only public abstract methods can exist in interfaces.
2) member variables in an abstract class can be of various types, while member variables in an interface can only be of type public static final.
3) the interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods.
4) A class can inherit only one abstract class, while a class can implement multiple interfaces.
two。 Differences at the design level
1) the abstract class is the abstraction of a thing, that is, the abstraction of the class, while the interface is the abstraction of the behavior. Abstract class abstracts the whole class, including attributes and behavior, but the interface abstracts the local (behavior) of the class. To take a simple example, airplanes and birds are different kinds of things, but they all have one thing in common, that is, they can both fly. At the time of design, the aircraft can be designed as an Airplane-like, and the bird as a Bird-like, but flight can not be designed as a class, so it is only a behavioral feature, not an abstract description of a class of things. At this point, you can design the flight as an interface Fly, including the method fly (), and then Airplane and Bird implement the interface Fly according to their own needs. Then as for different kinds of aircraft, such as fighter planes, civil aircraft and so on, they can directly inherit Airplane, which is similar to birds, and different kinds of birds can directly inherit Bird. It can be seen here that inheritance is a "yes or no" relationship, while interface implementation is a "yes or no" relationship. If a class inherits an abstract class, the subclass must be the kind of abstract class, and the interface implementation is related to whether the bird can fly (or whether it has the characteristic of flying). If you can fly, you can implement this interface, but if you can't fly, you can't implement this interface.
2) the design level is different, abstract class as the parent of many subclasses, it is a template design. The interface is a kind of behavior norm, it is a kind of radiation design. What is template design? The simplest example, we have all used the template inside ppt, if you use template A to design the common part of ppt B and ppt C ppt B and ppt C is template A, if their common part needs to be changed, then you only need to change template A, there is no need to change ppt B and ppt C. The radial design, such as an elevator with some kind of alarm, must be updated once the alarm is to be updated. That is to say, for an abstract class, if you need to add a new method, you can add a concrete implementation directly to the abstract class, and the subclass can not change; but not for the interface, if the interface has changed, then all classes that implement this interface must make changes accordingly.
Let's take a look at one of the most popular examples on the Internet: doors and alarms: both doors have two actions, open () and close (). At this time, we can define this 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 need the door to have the function of alarming alarm (), how do we do that? Here are two ideas:
1) put all three functions in the abstract class, but all the subclasses inherited from this abstract class have the alarm function, but some doors do not necessarily have the alarm function.
2) put all these three functions in the interface, and the class that needs to use the alarm function needs to implement open () and close () in this interface. Maybe this class does not have the functions of open () and close () at all, such as fire alarm.
From this, we can see that open (), close () and alarm () of Door belong to two different categories of behavior. Open () and close () belong to the inherent behavior characteristics of the door, while alarm () belongs to the extended additional behavior. Therefore, the best solution is to design the alarm as a separate interface, including alarm () behavior, and Door as a separate abstract class, including open and close behavior. Then design an alarm door 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 () {/ /.... }}
The above is how to understand the principle of Java interface and abstract class. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.