Interfaces and abstract classes

Source: Internet
Author: User

Does the interface inherit interfaces? Can an abstract class implement an (implements) interface? Does an abstract class inherit a concrete class (concrete Class)? Can there be static main methods in an abstract class?

. The difference between an abstract class and an interface

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes. There can be static main methods in an abstract class.

Note: As long as you understand the nature and role of interfaces and abstract classes, these questions are good answers, and you think, if you are the Java language Designer, will you provide such support, if not provided, what is the reason? If you have no reason not to provide, then the answer is yes.

The difference between an abstract class and an interface

Class that contains the abstract modifier is an abstract class, an instance object that the abstract class cannot create. A class containing an abstract method must be defined as an abstract Class,abstract class in which the method does not have to be abstracted. 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 give an error, but should not), but the abstract method 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 type 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.

And then again, the twoapplication onThe difference:

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 the servlets inherit this abstract base class, in the service method of the abstract base class, complete the permission judgment, record the access log and handle the exception code, in each subclass just complete their own business logic code, pseudo-code as follows:

Public abstract Classbaseservlet extends httpservlet{

Public final void Service (HttpServletRequest request,httpservletresponse response) throws Ioexcetion,servletexception {

Logging access logs

Make permission Judgments

if (with permissions) {

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, and are both professional and rigorous because it is intended for subclasses

}



public class MyServlet1 Extendsbaseservlet

{

Protected Voiddoservice (HttpServletRequest request, httpservletresponse response) throwsioexcetion,servletexception

{

Specific business logic code that this servlet only handles

}



}

The parent class method in the middle of the code is not certain, leaving the sub-class to do, the template method to design the pattern.

Note: The idea of this problem is to first explain the basic concepts of abstract classes and interfaces, then compare the grammatical details of both, and finally the application of the two differences. The difference between the two grammatical details is: First, from a classConstruction Method、Normal member variablesAndMethod(including abstract methods),Static VariablesAndMethod,Inheritance ofAnd so on 6 aspects to compare answers one by one, then from the perspective of the third party of the answer, especially at the end of a typical example to show their deep technical skills.

Interfaces and abstract classes

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.