java-based - the abstract classes and interfaces (rpm)

      Abstract classes and interfaces are two mechanisms java language to define the abstract concept, precisely because their presence was giving java a powerful object-oriented capabilities. They have support between the two abstract concepts very similar, even interchangeable, but there are differences.

 

      First, the abstract class
       we all know that everything is an object in an object-oriented areas, while all objects are described by class, but not all classes are used to describe an object. If a class does not have enough information to describe a specific object, and the need for other specific classes to support it, then this is the class we call it abstract. For example, new Animal (), we all know that this is generating an animal Animal object, but this particular Animal grow into what we do not know, it does not have a specific concept of animals, so he is an abstract class, we need a specific animal such as dogs, cats to carry out its specific description, we know it Chengshayang long.

      Since the field of object-oriented concept of abstract concepts in specific problem areas not corresponding to the abstract class it can not be characterized abstractions instantiated.

      Meanwhile, the abstract class embodies the idea of ​​data abstraction, is a mechanism to implement polymorphism. It defines a set of abstract methods, the specific manifestations of this group of abstract methods to have derived classes to implement. Meanwhile abstract class provides the concept of inheritance, any sense of its starting point is to inherit, otherwise it does not exist. So the definition of abstract class inheritance must be used, while in a chain of inheritance level abstract class nodes, leaf node must be a specific category. (I wonder if this understanding is wrong !!! expert guidance ....)

      When using an abstract class points to note:

         1, an abstract class can not be instantiated, instantiated work should be handed over to its subclasses to complete, it only needs to have a reference.

         2, abstract methods must be overridden by subclasses.

         3, as long as the abstract class contains an abstract method, the method must be defined as an abstract class, regardless of whether there are other ways also included.

         4, may comprise particular abstract class methods, of course, may not include abstract methods.

         5, abstract subclass of the same name can not be abstract methods of the parent class.

         6, abstract can not be modified with the final tied for the same class.

         7, abstract and not private, static, final tie with a native or modified method. ,

      Example:

      Define an abstract animal Animal, provides an abstract method called cry (), cats, dogs are a subclass of animal, because the cry () method is abstract, so Cat, Dog must implement cry () method. as follows:


abstract class Animal {public
public abstract void Cry ();
}

public class Cat the extends Animal {

@Override
public void Cry () {
System.out.println ( "meow: meow ...");
}
}

public class Dog {Animal the extends

@Override
public void Cry () {
System.out.println ( "dog: bark ...");
}

}

public class the Test {

public static void main (String [] args) {
Animal new new Cat A1 = ();
Animal Dog new new A2 = ();

a1.cry ();
a2.cry ();
}
}

------------------------- -------------------------------------------
the Output:
meow: meow meow ...
dog barking: barking ...


      Create abstract classes and abstract methods are very useful because they can make the abstract class of clear up, and tells the user how and compilers intend to use them. Abstract class is useful reconstructor, because they allow us to easily public The method moves upward along the inheritance hierarchy. (From: Think in java)

 

      Second, the interface
      interface is a more abstract than the abstract class of "class." Here to "like" the quotes that I can not find better words to express, but we must be clear point is, the interface itself is not a class, we can not be instantiated from one interface can be seen. As new Runnable (); certainly wrong, we can only achieve its new class.

      Protocol is used to establish an interface between the class and class, it provides a form only, without particular implementation. While achieving a class that implements the interface must implement all the methods of the interface by using the implements keyword in the class he represents a group or follow a specific interface, also said the "interface only its appearance, but now We need to declare how it works. "

      Interface is an extension of the abstract class, java data security can not be guaranteed multiple inheritance, that inheritance there is only one parent class, but different interfaces, a class can implement multiple interfaces at the same time, there is no relationship between these interfaces regardless so the interface can not make up for the abstract class multiple inheritance of defects, but it is recommended to use common inheritance and interfaces, as this could ensure data security and can implement multiple inheritance.

      In the course of using the interface needs to pay attention to the following questions:

         All parties access method 1, a Interface automatically be declared as public. Rather only to public, of course, you can display the declaration of protected, private, but the compiler will go wrong!

         2, the interface can define "member variables", or is immutable constants, because the interface of the "member variables" automatically changes to public static final. You can be accessed directly through the class name: ImplementClass.name.

         3, the method implemented in the interface does not exist.

         4, a non-abstract class that implements the interface must implement all of the methods of the interface. An abstract class can not achieve.

         5, can not be used to instantiate a new operator interface but an interface can declare a variable that must refer to (refer to) an object class that implements that interface. You can use instanceof to check whether an object implements a particular interface. For example: if (anObject instanceof Comparable) {}.

         6, when implementing multiple interfaces must avoid repeating the method name.

 

      Third, the difference between abstract classes and interfaces
      Although there between abstract classes and interfaces of the same larger point, and even sometimes can also be interchanged, but this does not make up for differences between them. The following syntax from the design level and two-level aspects of abstract classes and interfaces to elaborate.

      3.1 syntax level
      in the syntax level, java language for abstract classes and interfaces are given a different definition. Demo has the following classes to show differences between them at.

      Use abstract classes to implement:


public abstract class Demo {
abstract void method1();


void method2(){
//实现
}
}

       Use the interface to achieve

interface Demo {
void method1();
void method2();
}

       Abstract class, the abstract class can have any range of member data, but can also have their own non-abstract methods, but the interface mode, it can only have static data member can not be modified (but we are generally not in the interface use member data), while all its methods must be abstract. In a way, the interface is an abstract class specialization.
      Subclass is concerned, it can only inherit an abstract class (which is java for data security considerations), but can implement multiple interfaces.

      3.2 Design level
      just above the level of syntax and programming perspective to distinguish the relationship between them, these are low-level, to really good use of abstract classes and interfaces, we must distinguish from a higher level. It can only be seen from the perspective of the design concept of their essence. Generally they exist three different points are as follows:

      1, different levels of abstraction. Abstract class 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.

      2, different cross-domain. The cross-domain abstract class is a class with similar characteristics, and the interface cross-domain but different classes. We know that the abstract class is found in the public part of the subclass, and generalization to abstract class, subclass inherits the parent class can be, but different interfaces. Achieve its subclasses can not have any relationship in common. Such as cats, dogs, animals can be abstracted into a class abstract class, with a method called. Bird, aircraft can fly Fly implement interface, with flying behavior, where we can not be a bird, aircraft share a parent it! So abstract classes embodies a kind of inheritance, in order to make reasonable inheritance relationship must exist "is-a" relationship between the parent and the derived class, that is the parent class and derived classes in the concept of nature should be the same . For the interface is not, it does not require implementation of the interface and the interface defined by the concept is essentially the same, just to achieve a contract interface definition only.

      3, different design levels. For an abstract class, it is the bottom-up design, we must first know subclass to abstract superclass, and the interface is different, it does not need to know the existence of a subclass, you only need to define a rule As for what the subclass, when and how to achieve it knew nothing. For example, we only have a cat like here, if this is you then abstracted into a animal, is not design a bit excessive? We must be at least two animals, cats, dogs here, we have in common in their abstract form abstract class animal it! So abstract classes often come through reconstruction! But the interface is different, for example, fly, we simply do not know what this thing fly interfaces to achieve, how to achieve also unknown, we have to do is pre-defined interfaces good flying behavior. So that the abstract class is abstract from the bottom up, the interface is designed top-down.

      (The above is purely personal opinion, if different, the wrong place, hope you kind !!!!)

      In order to better distinguish between them forth, it will be explained using an example. This example is taken from: http: //blog.csdn.net/ttgjz/article/details/2960451

      We have a Door of abstraction, it has two acts open () and close (), then we can define to define this abstract concept through abstract classes and interfaces:

      Abstract class:


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

      interface

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

       As for other concrete classes can define an abstract class Door or define Door Implements the interface used by using extends, both found here and there is no big difference.
      But now if we need to have a door alarm function, then how to achieve it?

      Solution one: to increase a Door alarm method: clarm ();


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

       or

interface Door{
void open();
void close();
void alarm();
}

       This approach violates a core principle ISP object-oriented design (Interface Segregation Principle) - see annotation in the definition Door in the Door concept inherent in the behavior of the method and a concept further "alarm" behavior method mixed together . Such a problem is caused by those who rely solely on the Door module concept will change because the concept of "alarm" and change, and vice versa.
      Solution two

      Since the open (), close () and alarm () belong to two different concepts, then we separate them according to ISP principles defined in two represent two different concepts of abstract classes which, defined in three ways:

       1, both use an abstract class is defined.

      2, both used to define the interfaces.

      3, an abstract class definition, a definition of the interface is.

      Since java does not support multiple inheritance so the first one is not feasible. The latter two are possible, but what you choose reflects your understanding of the nature of the problem domain.

      If you choose the second interfaces are defined, then it reflects two problems: 1, we may not have a clear understanding of the problem domain, AlarmDoor in the end is also a door alarm on the concept of nature. 2, if our understanding of the problem domain is no problem, for example, we identified AlarmDoor in essence the concept is the same, then we have no right to reflect our design intent in the design in the analysis. Because you use two interfaces to be defined, define their concept does not reflect the above meaning.

     Third, if our understanding of the problem domain is this: the nature AlarmDoor Door, but it also has an alarm function of behavior, this time we just use the third option may be set forth in our design intent. AlarmDoor essentially are, which is why we use this concept to define abstract classes, while AlarmDoor with alarm function, indicating that it can complete the definition of the concept of behavioral function alarm, so alarm can use the interface to be defined. as follows:


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

interface Alarm{
void alarm();
}

class AlarmDoor extends Door implements Alarm{
void open(){}
void close(){}
void alarm(){}
}

       This implementation can basically reflect a clear understanding of our problem areas, the right reveals our design intent. In fact, the abstract class represents the "is-a" relationship, interface represents a "like-a" relationship, everyone in the selection can be used as a basis, of course, this is based on the understanding of the problem areas, such as: if we AlarmDoor is considered the concept is essentially the alarm, but also has the function of Door, then the above definitions will turn the way.
      annotation:

   ISP (Interface Segregation Principle): a core object-oriented principles. It shows that the use of multiple specialized interface than using a single interface to better overall.

   A class dependent on another class should be based on the smallest interface.

   An interface represents a role, should not be different roles are handed over to an interface. Not related interfaces merge together to form a large bloated interface, which is the pollution of the roles and interfaces.

      
      IV Summary
      1, the abstract class in java language represented an inheritance, a subclass of a parent class can only exist, but there may be multiple interfaces.

      2, in an abstract class can have its own member variables and non-abstract class method, but the interface can only exist in static data member immutable (but generally not defined in the interface data members) and all of its methods are Abstract.

      3, abstract classes and interfaces that reflect the design concept is different, represent an abstract class is "is-a" relationship, and the interface is represented by "like-a" relationship.

      Abstract classes and interfaces are java language in two different abstractions, their presence polymorphic provides very good support, although there are a lot of similarities between them. But for their choices often reflect your understanding of the problem domain. Only have a good understanding of the nature of the problem domain, in order to make correct and reasonable design.

 

Note: default method on a Java interface 8 of the original design goal is to be the interface already exists evolution - without the need to add new methods class that implements the interface had already existed to make any changes (even without recompiling) on You can use the new version of the interface.

Guess you like

Origin www.cnblogs.com/guanbin-529/p/11306191.html