Java abstract class and interface differences

Source: Internet
Author: User



Java abstract classes differ from interfaces:



Abstract class and interface are the two mechanisms in the Java language to support the definition of a class, and it is precisely because of the existence of these two mechanisms that it gives Java a powerful object-oriented capability. Abstract class and interface are very similar in terms of support for the definition of abstractions, and can even be replaced, so many developers are more likely to choose abstract classes and interface when they are doing the abstraction class definitions. In fact, there is a great difference between the two, for their choice even reflects the nature of the problem areas of understanding, the design intent of the understanding is correct, reasonable.



Abstract class and interface are used in the Java language for abstraction classes (the abstract class in this article is not translated from abstract class, it represents an abstract body, while abstract Class is a method of defining abstract classes in the Java language, which the reader is aware of, and what are abstract classes, and what are the benefits of using abstract classes?



In object-oriented concepts, all objects are depicted through classes, but not in the opposite way. Not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class. Abstract classes are often used to characterize the abstract concepts we derive from analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. For example, if we develop a graphical editing software, we will find that there are some specific concepts of circle and triangle in the problem domain, they are different, but they all belong to the concept of shape, the concept of shape does not exist in the problem domain, it is an abstract concept. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain.



Abstract classes are primarily used for type concealment in the object-oriented realm. We can construct an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all possible derived classes. The module can manipulate an abstract body. Because the module relies on a fixed abstraction, it can be modified, and the behavior of this module can be extended by deriving from this abstraction.



Abstract classes can have their own data members or non-ABSTARCT member methods, whereas in interface, an interface can only have static data members that cannot be modified (that is, it must be static final. However, data members are not typically defined in interface, and all member methods are abstract. In a sense, interface is a special form of abstract class.



First, the abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface. Perhaps this is a compromise of the Java language designer in considering Java's support for multiple inheritance.



Second, in the definition of abstract class, we can give the method the default behavior. However, in the definition of interface, the method does not have the default behavior, in order to circumvent this restriction, the delegate must be used, but this can add some complexity and sometimes cause a lot of trouble, and if the default behavior cannot be defined in the abstract class, it will cause the same method implementation to appear in the abstract class Each of the derived classes violates the "one Rule,one place" principle, resulting in code duplication, which is also detrimental to future maintenance. Therefore, you should be very careful when choosing between the abstract class and the interface.



Why the interface exists:



  Java is single-inheritance and does not support multiple inheritance, but with interfaces, Java can implement multiple interfaces. A class that implements an interface must implement all the methods declared within the interface (forced execution, even if it is an empty method).



Interface Features:



1, all methods within the interface are declared only, there is no method body (implicit declaration), for example: Public abstract void func ();



2, all variables within the interface are public static final by default, must be displayed initialized;



3, the interface does not have the method of construction, cannot be instantiated;



4, the implementation of the interface class to implement all the abstract methods, otherwise the class must be declared as abstract class, plus the abstract keyword;



5, interface can not implement interface (all interfaces are abstract methods), but the interface supports a multi-implementation!



Abstract class Features:



1. General variables and general methods are permitted in abstract class;



2, abstract class must have the keyword abstract;



3, abstract class inside can have no abstract method, but even so, also cannot instantiate (have abstract method must be abstract class);



4, abstract class can not be instantiated, but there is a method of construction (derived classes may be extended)



Abstract class Applications:



When multiple classes implement the same interface, each class implements all the methods of that interface (as above: Implementing an interface, all methods of the interface must be implemented). If there are several methods in the implementation of the implementation of the same class, then you can use abstract classes to separate these methods as a public method, by the abstract class to implement the interface, and then as long as the subclass inherited this abstract class is OK, no longer in each subclass to rewrite the public method.



Interface and abstract class application logic:



1, first write an interface, declare the implementation of this interface should do all the methods;



2, the common part of the implementation of this interface, with abstract classes to achieve, so that the abstract class has all the methods of the Declaration, as well as the implementation of public methods, simplifying the implementation of the interface;



3, derived classes as long as the inheritance and implementation of the abstract class ok!



The following is a code example:



declaring interfaces:

publicinterfaceInterExample {// declare an interface publicabstractvoidfunction1 (); publicabstractvoidfunction2 (); publicabstractvoidfunction3 (); publicabstractvoidpubFunct (); // public method declaration) declare an abstract class that implements the above interface: publicabstractclassAbsExample implementsInterExample {// implement public method publicvoidpubFunction () {System.out.println ("This is a public method of derived classes");}
}


Derived classes (inherited abstract classes):



publicclassExampleA extendsAbsExample {publicabstractvoidfunction1 () {System.out.println ("Method 1 used by A derived class personalization");) publicabstractvoidfunction2 () {System.out.println ("Method 2 used by A derived class personalization"); } Publicabstractvoidfunction3 () {System.out.println ("A derived method personalization method 3");}} publicclassExampleB extendsAbsExample {publicabstractvoidfunction1 () {System.out.println ("B derived method personalization method 1" );} publicabstractvoidfunction2 () {System.out.println ("Method 2 used by B-derived class personalization");} publicabstractvoidfunction3 () {System.out.println ("Method 3 used by B-derived class personalization"); }}





Public method Pubfunction (), implemented within the abstract class, Examplea,exampleb Common, there is no need to write in ExampleA, Exampleb two classes of the same implementation code. If the addition of the subclass Examplec,pubfunction () is different from the ExampleA and Exampleb, it would be nice to rewrite the method within the Examplec.






Summarize:



1, abstract classes and interfaces can not be directly instantiated, if you want to instantiate, abstract class variables must point to the implementation of all abstract methods of the subclass object, interface variables must point to the implementation of all interface methods of the class object;



2, abstract class to Quilt class inheritance, interface to be class implementation;



3, interface can only do method declaration, abstract class can do method declaration, can also do method to achieve;



4, the variables defined in the interface can only be public static constants, the variables in the abstract class are ordinary variables;



5. Abstract methods in abstract classes must all be implemented by the quilt class, if the subclass can not fully implement the parent class abstract method, then the subclass can only be abstract class. Similarly, when an interface is implemented, if the interface method cannot be fully implemented, then the class can only be an abstract class;



6, the abstract method can only affirm, cannot realize. abstract void ABC (); cannot be written as abstract void abc () {};



7, abstract class can have no abstract method;



8, if there is an abstract method in a class, then this class can only be abstract class;



9, the abstract method to be implemented, so can not be static, nor can it be private;



10, the interface can inherit the interface, and can inherit the interface more, but the class can only inherit one root.



Java abstract class and interface differences


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.