The difference between Java interface and abstract class

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 inheritance-level lining classes. Take Genericservlet in the JDK as an example:

1

2

3

4

5

6

7

8

9

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

}

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:

1

2

3

4

5

6

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

Parameter abstract class interface

The default method implementation can have a default method to implement an interface that is completely abstract. There's no way to implement it at all.

Implementing subclasses use the 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. Subclasses use the keyword implements to implement an interface. It needs to provide an implementation of all the declared methods in the interface

constructor abstract class can have constructor interfaces cannot have constructors

Unlike normal Java classes, except that you cannot instantiate an abstract class, it has no difference from the normal Java class. Interfaces are completely different types

The access modifier abstract method can have public, protected, and default modifiers for the interface method, which is public. You may not use other modifiers.

The Main method abstract method can have the main method and we can run it without the main method, so we cannot run it.

Multiple inheritance abstract methods can inherit one class and implement multiple interface interfaces can inherit only one or more other interfaces

Speed it's faster than the interface speed interface is slightly slower, because it takes time to find the method 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.

Original link: Javacodegeeks translation: Importnew.com-jessenpan

Link: http://www.importnew.com/12399.html

[Reprint please keep the source, translator and translation links.] ]

(RPM) The difference between Java interfaces and abstract classes

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: 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.