Java interface and abstract class instance analysis

Source: Internet
Author: User
Tags abstract inheritance

The examples in this article describe Java interfaces and abstract classes. Share to everyone for your reference. The specific analysis is as follows:

For object-oriented programming, abstraction is one of its major features. In Java, the abstraction of OOP can be embodied in two forms: interfaces and abstract classes. There are too many similarities and there are so many different places. Many people think they can be used interchangeably when they are beginners, but they are not. Today we'll come together to learn the interfaces and abstract classes in Java.

If there is any difference, please forgive and welcome the criticism, not very grateful.

I. Abstract class

Before you learn about abstract classes, let's take a look at the abstract methods. An abstract method is a special method: it has only declarations, but no concrete implementations. The declaration format for an abstract method is:

abstract void fun ();

Abstract methods must be decorated with the abstract keyword. If a class contains an abstract method, the class is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because an abstract class contains methods that do not have a specific implementation, you cannot create an object with an abstract class.

Here's a question: In the Java programming idea, you define an abstract class as "a class that contains abstract methods," but later you find that if a class does not contain abstract methods, it is also an abstract class if it is modified in abstract. This means that an abstract class does not necessarily have to contain abstract methods. Personally think this is a matter of concern, because if an abstract class does not contain any abstract methods, why also design as an abstract class? So remember this concept for a moment, not to delve into why.

?

1 2 3 [public] abstract class ClassName {abstract void Fun ();}

As you can see from here, an abstract class exists for inheritance, and if you define an abstract class and do not inherit it, it is tantamount to creating the abstract class in vain, because you cannot do anything with it. For a parent class, if one of its methods does not make sense in the parent class, it must be implemented differently depending on the actual requirements of the subclass, so that the method can be declared as an abstract method, at which point the class becomes an abstract class.

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

1 The abstract method must be public or protected (because if private, you cannot inherit the quilt class, the subclass will not implement the method), by default, 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 have an abstract method that implements the parent class, the subclass must also be defined as an abstract class.

In other ways, there is no difference between an abstract class and an ordinary class.

Two. Interface

interface, English called interface, in software engineering, interfaces refer to methods or functions that are called by others. From here, we can understand the Java language Designer's original intention, it is the abstraction of behavior. In Java, you set an interface in the following form:

?

1 2 3 [Public] interface InterfaceName {}

The interface can contain variables and methods. Note, however, that the variables in the interface are implicitly specified as public static final variables (and only public static final variables, which are reported to compile errors with private adornments), and methods are implicitly specified as public The abstract method can only be public abstract (with other keywords, such as private, protected, static, final, etc.), and all the methods in the interface cannot have a specific implementation, that is to say, The methods in the interface must all be abstract methods. From here you can see the difference between the interface and the abstract class, the interface is an extremely abstract type, it is more "abstract" than the abstract class, and generally does not define variables in the interface.

To have a class follow a specific set of interfaces, you need to use the Implements keyword, which is as follows:

?

1 2 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, all the methods in that interface must be implemented. Abstract methods in this interface are not implemented for abstract classes that follow an interface.

Three. The difference between abstract classes and interfaces

1. Differences on the grammatical level

1 abstract class can provide the implementation details of the member method, while the interface can only exist public abstract method;

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

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 may implement multiple interfaces.

2. Design-level differences

1 abstract class is an abstraction of a thing, that is, a class abstraction, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including attributes, behavior, but an interface that abstracts the local (behavior) of the class. For a simple example, airplanes and birds are different kinds of things, but they all have one thing in common, they all fly. Then in the design, the aircraft can be designed as a class airplane, the birds are designed as a class bird, but can not fly this feature is also designed as a class, so it is only a behavioral characteristics, not the abstract description of a class of things. The flight can then be designed as an interface fly, containing the method fly (), and then airplane and bird implement fly this interface separately according to their own needs. Then as for different kinds of aircraft, such as fighter jets, civil aircraft, such as direct inheritance airplane can, for birds are similar, different kinds of birds directly inherit bird class can. As can be seen from here, inheritance is a "is not" relationship, and interface implementation is "there is no" relationship. If a class inherits an abstract class, then the subclass must be the kind of abstract class, and the interface implementation is there is no, have no relationship, such as whether the bird can fly (or whether it has the characteristics of flight), can fly can implement this interface, can not fly do not implement this interface.

2 The design level is different, abstract class as many subclasses of the parent class, it is a template design. And the interface is a code of conduct, it is a kind of radiant design. What is a template design? The simplest example, we have used the template in the PPT, if the template a designed ppt B and ppt c,ppt B and PPT C is the public part of the template A, if they need to change the public parts, only need to change the template A on it, do not need to be a new PPT B and PPT c to make changes. And the radiant design, such as a certain elevator installed some kind of alarm, once you want to update the alarm, you must update all. That is to say, for an abstract class, if you need to add a new method, you can add a specific implementation directly to the abstract class, and the subclass can not make changes, but not for the interface, and if the interface is changed, all classes that implement this interface must be changed accordingly.

Here's one of the most popular examples of the Web: doors and Alarms: doors have open () and close () two actions, at which point we can define this abstract concept through abstract classes and interfaces:

?

1 2 3 4 Abstract class Door {public abstract Void open (), public abstract void Close ();}

Or:

?

1 2 3 4 Interface Door {public abstract Void open (), public abstract void Close ();}

But now if we need a door with alarm alarm (), how do we implement it? Here are two ways to do this:

1 The three functions are placed in the abstract class, but so all the subclasses inherited from this abstract class have the alarm function, but some doors do not necessarily have the alarm function;

2 The three functions are placed in the interface, the need to use the alarm function of the class need to implement the interface Open () and close (), perhaps this class does not have the open () and close () these two functions, such as fire alarm.

It can be seen from here that the door open (), close () and alarm () are essentially two different categories of behavior, open () and close () are intrinsic to the door itself, and alarm () is an extension of the additional behavior. The best solution, therefore, is to design the alarm individually as an interface, containing the alarm () behavior, door designed as a separate abstract class that contains open and close two behaviors. Then design an alarm door to inherit the door class and implement the alarm interface.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 Interface Alram {void alarm ();} abstract class Door {void Open (); void close ();} class Alarmdoor extends Door implemen TS Alarm {void oepn () {//...} void Close () {//...} void Alarm () {//...}}

I hope this article will help you with your Java programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.