The difference between Java abstract class and interface

Source: Internet
Author: User

Tags: cte default ali resolves add nbsp cell multiple inheritance ADO

Many of the common face questions will be such as what is the difference between an abstract class and an interface, what situations will use abstract classes, and what situations you will use to interface such problems. We will discuss these topics in detail in this article.

Before discussing the differences between them, let's look at the characteristics of abstract classes and interfaces.

Abstract class

Abstract classes are used to capture the generic properties of subclasses. It cannot be instantiated and can only be used as a superclass of subclasses. Abstract classes are templates that are used to create inheritance-level lining classes. Take Genericservlet in the JDK as an example:

123456789 public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {    // abstract method    abstract void service(ServletRequest req, ServletResponse res);    void init() {        // Its implementation    }    // other method related to Servlet}

When the HttpServlet class inherits Genericservlet, it provides the implementation of the Service method:

123456789101112131415 public class HttpServlet extends GenericServlet {    void service(ServletRequest req, ServletResponse res) {        // implementation    }     protected void doGet(HttpServletRequest req, HttpServletResponse resp) {        // Implementation    }    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {        // Implementation    }    // some other methods related to HttpServlet}

An interface is a collection of abstract methods. If a class implements an interface, it inherits the abstract method of the interface. This is like a contract pattern, and if you implement this interface, you must ensure that you use these methods. An interface is just a form, and the interface itself cannot do anything. Take the Externalizable interface as an example:

123456 publicinterface Externalizable extends Serializable {     void writeExternal(ObjectOutput out) throws IOException;     void readExternal(ObjectInput in) throwsIOException, ClassNotFoundException;}

When you implement this interface, you need to implement the above two methods:

12345678910111213141516171819 public class Employee implements Externalizable {    int employeeId;    String employeeName;    @Override    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {        employeeId = in.readInt();        employeeName = (String) in.readObject();    }    @Override    public void writeExternal(ObjectOutput out) throws IOException {        out.writeInt(employeeId);        out.writeObject(employeeName);    }}
Comparison of abstract classes and interfaces
Parameters Abstract class Interface
The default method implementation It can have the default method implementation The interface is completely abstract. There's no way to implement it at all.
Realize Subclasses use the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide an implementation of all the declared methods in the abstract class. Subclasses use the keyword implements to implement an interface. It needs to provide an implementation of all the declared methods in the interface
Constructors Abstract classes can have constructors Interfaces cannot have constructors
Differences from normal Java classes Except that you can't instantiate an abstract class, it's no different than a normal Java class. Interfaces are completely different types
Access modifiers Abstract methods can have public,protected , and default modifiers The interface method default modifier is public. You may not use other modifiers.
Main method The abstract method can have the main method and we can run it The interface does not have a main method, so we cannot run it.
Multiple inheritance An abstract method can inherit a class and implement multiple interfaces Interfaces can inherit only one or more other interfaces
Speed It's faster than the interface. The interface is a little bit slower because it takes time to look for methods implemented in the class.
Add a new method If you add a new method to an abstract class, you can give it a default implementation. So you don't need to change your current code. If you add a method to an interface, you must change the class that implements the interface.
When to use abstract classes and interfaces
    • If you have some methods and want some of them to have default implementations, then use abstract classes.
    • If you want to implement multiple inheritance, then you must use the interface. Because Java does not support multiple inheritance , subclasses cannot inherit multiple classes, but can implement multiple interfaces. So you can use the interface to solve it.
    • If the basic ability is constantly changing, then you need to use abstract classes. If you constantly change the basic functionality and use the interface, you need to change all classes that implement the interface.
Default methods and static methods in Java8

Oracle has tried to reduce the differences between abstract classes and interfaces by introducing default and static methods into the interface. Now, we can provide the interface with a default implementation method and not enforce the subclass to implement it. I will explain this in the next blog post.

The difference between Java abstract class and interface

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: