Introduction to the difference between Java abstract class interface _java

Source: Internet
Author: User
Class that contains the abstract modifier is an abstract class, an instance object that the abstract class cannot create. A class that contains an abstract method must be defined as a method in the abstract Class,abstract class classes that does not have to be abstract. Abstract methods defined in the abstract class class must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static methods. If a subclass does not implement all of the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type.
An interface (interface) can be said to be 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.
Let's compare the grammatical differences between the two
1. Abstract classes can have construction methods, and interfaces cannot have construction methods.
2. An abstract class can have ordinary member variables, there are no ordinary member variables in the interface
3. Abstract classes can contain generic methods that are not abstract, and all methods in an interface must be abstract and not have a generic method that is not abstract.
4. The access type of an abstract method in an abstract class can be public,protected and (the default type, although
Eclipse does not give an error, but it should not, but an abstract method in an interface can only be of public type, and the default is the public abstract type.
5. The abstract class can contain static methods, and the interface cannot contain static methods
6. 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 default is the public static final type.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
Let's talk about the difference between the two in the application
Interface is more in the system architecture design methods play a role, mainly used to define the communication contract between the modules. and abstract classes play a role in code implementation, you can implement code reuse, for example, template method design patterns are a typical application of abstract classes, assuming that all servlet classes for a project have the same way of determining permissions, logging access logs, and handling exceptions, you can define an abstract base class, Let all the servlet inherit this abstract base class, in the abstract base class service method to complete the right to judge, record access log and handling the exception code, in each subclass is just complete their own business logic code, pseudocode as follows:
Copy Code code as follows:

Public abstract class Baseservlet extends HttpServlet
{
Public final void Service (HttpServletRequest request, httpservletresponse response) throws Ioexcetion,servletexception
{
Record Access log
Make permission Judgments
if (with permissions)
{
Try
{
Doservice (Request,response);
}
catch (Excetpion e)
{
Logging exception information
}
}
}
protected abstract void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, Servletexception;
Note that access rights are defined as protected, which is both professional and rigorous, because it is used specifically for subclasses.
}
public class MyServlet1 extends Baseservlet
{
protected void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion,servletexception
{
Specific business logic code that this servlet handles only
}
}

A section of code in the middle of the parent class is not sure, leaving the subclass to dry, using the template method to design the pattern.
Notes: The idea of this problem is to first explain the basic concepts of abstract class and interface, then compare the grammatical details of the two, and finally say the application of the difference between the two. The difference between the grammatical details of the two is: first, from the construction method in a class, ordinary member variables and methods (including abstract methods), static variables and methods, inheritance and so on 6 aspects to compare the answer, and then from the perspective of the third party to inherit the answer, especially in the end with a typical example to show their deep technical skills.

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.