Depth understanding of abstract classes and interfaces

Introduction:

Object-oriented programming, the abstract is one of its major characteristics. In Java, abstract OOP may be embodied in two forms: abstract classes and interfaces. Both have too many similarities, there are too many different places. Many people will think beginners when they are free to be used interchangeably, but actually is not. Today we take a look at learning in Java interfaces and abstract classes.

 

First, the abstract class

Before understanding abstract classes, first look at abstract methods. Abstract method is a special method: it is only a statement, but not the implementation. Statement format abstract method is as follows:

abstract void fun();

 

Abstract methods must be modified with the abstract keyword. If a class contains abstract methods, the class is called abstract class, abstract class must be modified before the class with the abstract keyword. Because no specific method of the abstract class contains implemented, it can not create object abstract class.

The following should pay attention to the question: "JAVA programming ideas," a book, the abstract class defined as "contains an abstract method of class," but later found, but modified with abstract words is an abstract class if a class does not contain abstract methods. That is an abstract class does not have to contain abstract methods. Personally I feel that part of the problem into a dead end, because if an abstract class does not contain any abstract methods, why design is an abstract class? So let us remember this concept it is not necessary to go into why.

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

As can be seen from here, it is to inherit the abstract classes exist, if you define an abstract class, but not to inherit it, then equal in vain to create this abstract class because you can not use it for anything. For a parent, if it's a way to achieve it in the parent class does not make sense, must be implemented in accordance with the actual needs of different subclasses, then this method can be declared as abstract methods, the class also at this time It has become the abstract class.

It contains abstract methods of the class is called abstract class, but that does not mean an abstract class can have only abstract methods, and general category, as it also can have an ordinary member variables and member methods. Note that there are three points difference between abstract classes and general classes:

    • 1) abstract method must be public or protected (because if it is private, it is not inherited by subclasses, subclasses can not implement this method), default by default public.

    • 2) An abstract class can not be used to create objects;

    • 3) If a class inherits from an abstract class, the subclasses of the abstract parent class must implement. If the subclass does not realize the abstract parent class method, it must also be defined as a subclass of an abstract class.

    • In other respects, the abstract class and normal class and there is no difference.

Second, the interface

Interface, known as the English interface, software engineering, refers to the interface methods and functions for others to call. From here, we can appreciate the goals of the Java language designers, it is an abstract behavior. In Java, a given form of the following interfaces:

[public] interface InterfaceName {

}

Interfaces may contain variables and methods. Note, however, the interface variables are implicitly designated as public static final variables (and only public static final variable, the compiler will report an error with private modification), and methods will be implicitly designated as public abstract method and the only public abstract method (with additional keywords, such as private, protected, static, final and other modifications will report compilation errors), and the interface method does not have all the specific implementation, that is, interface methods must They are abstract. From here we can see the difference between vaguely abstract classes and interfaces, the interface type is an extremely abstract, it is more "abstract" than an abstract class, and the variable is not defined in the interface under normal circumstances.

Let the class follow a certain set of interfaces specifically require the use of implements keyword, the following format:

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

As can be seen, to allow a plurality of classes follow a specific interface. If a non-abstract class followed an interface, the interface on all methods must be implemented. For follow an abstract interface classes, abstract methods of the interface can not be achieved.

The difference between abstract classes and interfaces

1, the difference between the grammatical level

  • 1) An abstract class can provide the implementation details of a method, which methods public abstract interface and can exist;

  • Member variable 2) abstract class may be various types, but the interface member variables can only be public static final type;

  • 3) the interface can not contain a static block and a static method, a static abstract class can have static methods and code blocks;

  • 4) A class can inherit an abstract class, but a class can implement multiple interfaces.

2, the difference between the design level

1) An abstract class is an abstraction of a thing, that is an abstract class, and the interface is an abstract behavior. Abstract class is an abstract class for the entire whole, including attributes, behavior, but the interface is based on the partial (behavior) abstract. Here is a simple example, aircraft and birds are different kinds of things, but they all have one thing in common, that is, will fly. So in the design, the aircraft can be designed as a class Airplane, the birds designed as a class Bird, but you can not fly this feature is also designed as a class, so it's just a characteristic behavior, not an abstract description of a class of things . At this point you can fly an interface designed to Fly, it contains the method fly (), and then Airplane Fly Bird implement this interface, respectively, according to their needs. As there are different types of aircraft and then, such as fighter aircraft, civil aircraft can be directly inherited Airplane, the bird is similar for different species of birds can be directly inherited Bird class. As can be seen from here, is a succession "is not" relationship, while the interface is "there is no" relationship. If a class inherits an abstract class, subclass must be a kind of abstract class and interface is there, with the relationship does not have, such as if a bird can fly (flight or whether they have this feature), can fly you can implement this interface, it can not fly does not implement this interface.

Different 2) the design level, abstract class as many sub-class of the parent class, it is a template design. The interface is a code of conduct, it is a radiant design. What is a template design? The most simple example, we have used inside ppt templates, if designed ppt B and ppt C A template, ppt B and ppt C common template A part is, if they are part of the public need to change, you only need to change a template can, and do not need to re-ppt B and ppt C to make changes. The design of radiation, such as an elevator have installed some kind of alarm, once the alarm to be updated, it must all be updated. That is for the abstract class, if you need to add new methods can be added directly in the concrete realization of the abstract class, subclass can not be changed; and for the interface is not, if the interface has been changed, all the realization of this interface class must make the appropriate changes.

A look at the following examples of the most widely circulated on the Internet: Examples and door alarms: doors are open () and close () two actions, then we can define to 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 to have a door alarm function, then how to achieve? We provided the following two ideas:

1) these three features are placed inside an abstract class, but this way all inherited from the subclass of the abstract class are equipped with alarm functions, but some doors do not necessarily have the alarm function;

2) these three features are placed inside the interface, you need to use the alarm function of the class would need to implement this interface open () and close (), perhaps this class simply do not have open () and close () This two functions, such as a fire alarm.

As can be seen here, the Door Open (), close (), and alarm () simply acts within the part of two different areas, open (), and close () belong to the phylum inherent behavioral characteristics, and alarm () belongs extending additional behavior. Therefore the best solution is to design a separate alarm interface comprising alarm () acts, as a separate Door an abstract class, comprising open and close two behaviors. Redesign inherit a door alarm Door Alarm classes and implement interfaces.

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

Original: www.cnblogs.com/dolphin0520/p/3811437.html

Guess you like

Origin www.cnblogs.com/zhengjinsheng/p/11199700.html