Differences between Java Abstract classes and interfaces and java Abstract classes

Source: Internet
Author: User

Differences between Java Abstract classes and interfaces and java Abstract classes

For many common interview questionsWhat is the difference between an abstract class and an interface? Under what circumstances will the abstract class be used?This is a problem. This article will discuss these topics carefully.

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

Abstract class

Abstract classes are used to capture the general characteristics of sub-classes. It cannot be instantiated and can only be used as a subclass of A superclass. An abstract class is a template used to create subclasses in the inheritance level. Take GenericServlet in JDK as an example:


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:


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 set of abstract methods. If a class implements an interface, it inherits the abstract method of the interface. This is like the contract mode. If this interface is implemented, you must ensure that these methods are used. 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 between abstract classes and interfaces
Parameters Abstract class Interface
Default method implementation It can be implemented by default. The interface is completely abstract. There is no method implementation at all.
Implementation Subclass usageExtendsTo inherit the abstract class. If the subclass is not an abstract class, it must provide the implementation of all declared methods in the abstract class. Subclass keywordImplementsTo implement the interface. It must provide implementation of all declared methods in the interface
Constructor Abstract classes can have Constructors The interface cannot have a constructor.
Differences from normal Java classes Except that you cannot instantiate an abstract class, it is no different from a normal Java class. Interfaces are completely different types
Access Modifier Abstract methods can be:Public,ProtectedAndDefaultThese Modifiers The default modifier of the interface method isPublic. You cannot use other modifiers.
Main Method Abstract METHODS can have the main method and we can run it. The interface does not have the main method, so we cannot run it.
Multi-Inheritance Abstract METHODS can inherit a class and implement multiple interfaces. An interface can inherit only one or more other interfaces.
Speed It is faster than the interface speed The interface is a little slow, because it takes time to find the methods implemented in the class.
Add Method If you add a new method to the abstract class, you can provide it with the default implementation. Therefore, you do not 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, use the abstract class.

  • If you want to implement multi-inheritance, you must use interfaces. BecauseJava does not support multi-inheritance.Subclass cannot inherit multiple classes, but can implement multiple interfaces. Therefore, you can use interfaces to solve it.

  • If the basic functions are constantly changing, you need to use abstract classes. If you constantly change basic functions and use interfaces, you need to change all classes that implement the interfaces.

Default and static methods in Java 8

Oracle has begun to introduce default and static methods to interfaces to reduce the differences between abstract classes and interfaces. Now, we can provide a default Implementation Method for the interface and do not need to force subclass to implement it. This type of content will be elaborated in future articles.

Head of Java

No.: javatuanzhang

Sharing Java technical expertise on a daily basis

Long-pressed QR code recognition

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.