Java abstract class and interface

Source: Internet
Author: User

the difference between abstract class and interface
The class that contains the abstract modifier is abstract, and the abstract class cannot create an instance object。A class containing an abstract method must be defined as abstract class,abstract the method in class is not necessarily an abstractionIn the.????????????????. abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.
???? An interface (interface) can be described as a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.

Here's a comparison of the syntax differences between the two:
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. Abstract methods in an abstract class can have access types public,protected and (the default type, although Eclipse does not error, but not), but the abstract methods in the interface can only be of the public type, and the default is the public abstract type.
5.?abstract classes can contain static methods, and interfaces cannot contain static methods
6.?both abstract classes and interfaces can contain static member variables, and the access types of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public static final type is the default.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
The difference between the two applications is:
???? Interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules. Abstract classes play a role in the implementation of code, can implement code reuse, for example, template method design pattern is a typical application of abstract class, it is assumed that all the servlet classes of a project to use the same way to determine permissions, log access logs and handle exceptions, you can define an abstract base class, Let all servlets inherit this abstract base class, and in the service method of the abstract base class, complete the permission judgments, log access logs, and code that handles exceptions, and only complete their own business logic code in each subclass. The pseudo code is as follows:
Public abstract class Baseservlet extends httpservlet{
Public
final void service (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, servletexception{
Log
access logs
for permission to determine
if (with permission) {
??
??? try{
???? doservice (Request,response);}
catch (excetpion e) {
????
Log exception information
}
? ? ? ? ? ?}
? ? ? ? ? ?}
protected abstract void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, servletexception;//Note that access rights are defined as protected, rigorous, because it is intended for subclasses
}
public class MyServlet1 extends baseservlet{
protected void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, servletexception{????? This servlet only handles specific business logic code}
}
???? In the middle of the parent class of the code is not sure, leave the sub-class dry, useTemplate Method Design pattern。?

Java abstract class and interface

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.