Java Abstract class and interface differences and examples, java Abstract

Source: Internet
Author: User

Java Abstract class and interface differences and examples, java Abstract
Statement: This article from the http://www.enet.com.cn/article/2007/1126/A20071126923475.shtml in the article

Java provides two support mechanisms for the definition of abstract classes:

Abstract class and interface. Let's take a look at the differences between the two mechanisms?

0. The following points must be clearly stated here: 1. abstract Classes define that such allocation instances are not allowed to be in the memory. abstract method: There is a modifier abastract 3 before the method definition (Declaration. common method: There is no modifier abastract before the method definition (Declaration. if a method or variable does not add any access permission modifier, It is the default access permission. In this case, members in the same package can access it. the Java access permissions are private <protected <defualt <public, and are compared in three aspects: 1. In terms of syntax definition, abstract class and interface define an abstract class named Demo as an example to illustrate this difference. The following describes how to use abstract class to define Demo abstract classes: abstract class Demo {abstract void method1 (); abstract void method2 (); // some other abstract methods // -- espc. // may has none-abstract methods in it void none-abstract-method1 ();} use the interface method to define the Demo abstract class as follows: interface Demo {void method1 (); void method2 ();} Description: 1. A method can contain member data, abstract methods, and common methods. the class does not restrict the access permissions of its components. in the interface mode, the access permissions of its components are limited. the access permission must be greater than protec. Ted (can only be default or public) 1) If this interface contains member data, it must be a constant modified by static final. 2) If this interface contains member methods, it must be the abstract public method (meaning all member methods are abstract). In a sense, interface is a special form of abstract class. 2. the relationship between an abstract class and an abstract method is: if a class has an abstract method, this class must be an abstract class. At this time, the abastract modifier must be used before the class definition, abstract classes do not necessarily contain abstract methods. it can only contain common method 2. abstract class and interface 1 from the programming level. abstract class is an inheritance relationship (that is, the relationship between the parent and the child). A class can only use an inheritance relationship once. However, a class can implement multiple interfaces (implements. 2. in abstract class In the definition, we can assign the default behavior of the method. However, in the interface definition, the method cannot have default behavior. the inability to have default behaviors in abstract classes makes daily code maintenance after release difficult. iii. abstract class and interface <essential differences> 1. abstarct class represents an inheritance relationship. The parent class and the derived class are essentially the same concept. to make the inheritance relationship reasonable, the parent class and the derived class must have a "is a" relationship. 2. for an interface, the implementer and interface definitions of the interface are not required to be consistent in concept. it only requires the implementation of the interface definition contract. 4. instance description in order to make the discussion easier to understand, the following describes a simple example. Consider this example. Suppose there is an abstract concept about the Door in our problem field. The Door has two actions: open and close. in this case, abstract class or interface can be used to define a type that represents the abstract concept. suppose the definitions are as follows: use abstract class to define Door: abstract class AbsDoor {abstract void open (); abstract void close ();} use interface to define Door: interface IDoor {void open (); void close ();} for other specific Door types, extends can use the AbsDoor or implements defined in the abstract class method to use the IDoor defined in the interface method. At this time, it seems that abstract class and interf are used. There is no big difference between ace. If there is a need to add functions in the future, Door should also have the alarm function. How should we design the class structure for this example? Below we will list possible solutions and analyze these different solutions from the design concept layer. 1. solution 1: simply add an alarm Method to the definition of AbsDoor, as follows: abstract class AbsDoor {abstract void open (); abstract void close (); abstract void alarm () ;}or interface IDoor {void open (); void close (); void alarm () ;}then the AlarmDoor with alarm function is defined as follows: class AlarmDoor extends AbsDoor {void open (){...} Void close (){...} Void alarm (){...} Or class AlarmDoor implements IDoor {void open (){...} Void close (){...} Void alarm (){...} This method violates a core principle ISP (Interface Segregation Priciple) in object-oriented design ). because the inherent behavior methods of the Door concept and the behavior methods of another concept "alarm" are mixed in the definition of the Door. one problem is that the modules that rely solely on the Door concept will change because of the change in the concept of "alarm. 2. solution 2: Since open, close, and alarm belong to two different concepts, they should be defined in abstract classes that represent these two concepts according to the ISP principle. definition Methods:. both concepts use abstract class to define B. both concepts define C. one concept is defined using abstract class, and the other is defined using interface. However, Java does not support multiple inheritance. therefore, the two A methods are not feasible. both methods are feasible. however, their selection reflects Our understanding of the concept nature in the problem field and the reflection of the design intent are correct and reasonable. If Method B is defined, there are two problems: 1. We do not understand the problem field clearly: Is the concept of AlarmDoor actually a Door or an alarm? 2. If we have no problem understanding about the problem field, for example, we have found that AlarmDoor is essentially consistent with Door through analysis of the problem field. therefore, our design intent cannot be correctly revealed during implementation, because the definitions of these two concepts (using B) do not reflect the above meanings. At this point, we can clearly understand that AlarmDoor is essentially a Door, and it also has the alarm function. abstract class represents an inheritance relation, which is essentially a "is a" relation. for Door, we should use the abstarct class method to define it. In addition, AlarmDoor has the alarm function. it indicates that it can complete the behaviors defined in the alarm concept, so the alarm concept can be defined through interface. this implementation method can clearly reflect our understanding of the problem field and correctly reveal our design intent. the final design is 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 (){...} Conclusion: abstract class represents the "is a" relation, and interface represents the "like a" relation. You can use it as a basis for your selection, of course, this is based on the understanding of the problem field. for example, if we think that AlarmDoor is essentially an alarm and has a Door function, the above definition method will be reversed. abstract class and interface are two methods of defining abstract classes in Java. They have great similarity. because they represent different relations between concepts, their choices often reflect the understanding of the concept nature in the problem field. is the reflection of design intent correct and reasonable.
What are the differences between abstract classes and interfaces in java?

To put it simply,
The interface is public, and there cannot be private methods or variables in it. They are used by others. abstract classes can have private methods or private variables,

In addition, the interface must implement all the methods defined in the interface, and the methods to be used can be selectively rewritten to implement the abstract class. In general applications, the top level is the interface, then the abstract class implements the interface, and the class is implemented.

In addition, an interface can implement multi-inheritance, while a class can only inherit one super class, but it can implement multi-inheritance by inheriting multiple interfaces. The interface also has an identifier (there is no method in it, such as the Remote interface) and Data Sharing (the variables inside are constants.

In Java, the differences between interfaces and abstract classes are briefly described)

Interfaces in Java
Because multi-inheritance is not allowed in Java, You can implement multiple interfaces to implement the functions of multiple classes.
Java interfaces and Java Abstract classes represent abstract types, which are the specific manifestations of the abstraction layer we need to propose. To improve the Reuse Rate of programs, increase the maintainability and scalability of programs, the OOP object-oriented programming must be interface-oriented and abstract-oriented, correctly Use interfaces and abstract classes as the top layers of your structural hierarchy.
There are too many similarities between Java interfaces and Java Abstract classes, and there are too many special points. What exactly is the best place for Java interfaces and Java Abstract classes? By comparing them, you can find out.
1. The biggest difference between Java interfaces and Java Abstract classes is that Java Abstract classes can provide partial implementation of some methods, but Java interfaces cannot. This is probably the only advantage of Java Abstract classes, however, this advantage is very useful. If a new concrete method is added to an abstract class, all its subclasses will get this new method at once, and the Java interface cannot do this, if you add a new method to a Java interface, all classes that implement this interface cannot be compiled successfully, because you must make every class implement this method again, this is obviously a disadvantage of the Java interface.
2. The implementation of an abstract class can only be given by the subclass of this abstract class. That is to say, this implementation is in the hierarchy defined by the abstract class. Due to the single inheritance of the Java language, therefore, the efficiency of an abstract class as a type definition tool is greatly reduced. At this point, the advantages of the Java interface come out. Any class that implements the method specified by a Java interface can have the type of this interface, A class can implement any number of Java interfaces, so this class has multiple types.
3. From the 2nd point, it is not difficult to see that the Java interface is an ideal tool for defining the hybrid type. The mixed class indicates that a class has not only a primary type but also other secondary behaviors.

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.