The difference between Java abstract class and interface

Source: Internet
Author: User
Tags modifiers

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 subclasses in the inheritance hierarchy. Take Genericservlet in the JDK as an example:


Public abstract class Genericservlet implements Servlet, ServletConfig, Serializable {//abstract method Abstract V     OID 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:

public class HttpServlet extends Genericservlet {void Service (ServletRequest req, servletresponse res) {//Imp    Lementation} protected void Doget (HttpServletRequest req, HttpServletResponse resp) {//implementation } protected void DoPost (HttpServletRequest req, HttpServletResponse resp) {//implementation}//some Other methods related to HttpServlet}

Interface


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:

Public interface Externalizable extends Serializable {void writeexternal (ObjectOutput out) throws IOException; void Readexternal (ObjectInput in) throws IOException, ClassNotFoundException;}

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

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 implements the It can have the default method to implement The interface is completely abstract. It doesn't exist at all. The implementation of the method
implement extends keyword to inherit an abstract class. If the subclass is not an abstract class, it needs to provide an implementation of all the declared methods in the abstract class. implements to implement the interface. It needs to provide implementations of all declared methods in the interface
constructor Abstract classes can have constructors The interface cannot have a constructor
differs from normal Java class except you can't instantiate an abstract class, it doesn't have any difference from the normal Java class The interface is completely different type
access modifier public , protected , and default these modifiers public . You may not use other modifiers.
main method There is no main method for the interface, so we cannot run it.
multiple inheritance Abstract methods can inherit a class and implement multiple interfaces The interface can inherit only one or more other interfaces
speed It's faster than the interface speed 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 do you 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.



The difference between 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.