The difference between abstract classes and interfaces in Java (reading notes)

Source: Internet
Author: User
Tags modifiers

Abstract classes and interfaces for Java


Abstract classes and interfaces are two confusing concepts, but one sentence can be very good to distinguish between the two: abstract class is the abstraction of things, and the interface is the abstraction of the behavior. Abstract class abstract is a kind of things, things contain some intrinsic properties and behavior, and the interface is more to provide intrinsic property behavior beyond the extension behavior. For example, animals can be seen as abstract classes with some intrinsic properties, such as height and weight, and some common behaviors such as eating and sleeping, all of which follow these attributes and behaviors. In addition, some animals can fly, but not all animals have the ability to fly, when the flight is only the extension of the function, equivalent to an interface.

I. Abstract class
Abstract classes are classes that contain abstract methods. An abstract method is a special method in which the declared format of an abstract method is:

abstract void Fun ()

Abstract classes have the following three main features:
1) It only declares that there is no specific implementation. Both abstract classes and abstract methods must be described by the abstract keyword.
2) The specific implementation of the abstract method is done by subclasses, and if the subclass does not implement an abstract class in the parent class, the subclass also needs to be defined as an abstract class. Therefore, the access modifier of the abstract method can only be public and protected, which is well understood, if the access permission is private, inheriting the subclass of the abstract class cannot be specific to the method.
3) Abstract classes cannot be instantiated. Because an abstract method in an abstract class is not specifically implemented, calling the method after instantiation does not have a specific operation. If you want to invoke a non-abstract method in an abstract class, you can instantiate the subclass of the abstract class and call it. (assuming, of course, that subclasses are not abstract classes)
In addition, although the definition of an abstract class is a class that contains abstract methods, it does not imply that there can be only abstract methods in this abstract class. As with normal classes, you can have member variables and normal member methods in an abstract class.

The three-point difference between abstract classes versus ordinary classes:
1) The abstract method must be public or protected (if it is private, it cannot be inherited by subclasses, and thus the method is implemented), by default, public
2) Abstract classes cannot be instantiated
3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class, otherwise the subclass is also an abstract class

Two. Interface
The modifier is interface, in software engineering, the interface refers to the method for others to call, because we can also initially understand the original intention of the Java language designer, that is, the interface is the abstraction of the extension behavior. The declaration format for an interface is:

public interface InterfaceName
{
}

The interface has the following three features:
1) The interface is callable. This is also the meaning of interface existence
2) The variables in the interface are implicitly specified as public static final (and can only be public static final, with private compilation error). However, the variables are not generally defined in the interface (since all methods are not implemented, there is no way to define variables.)
3) The method is implicitly specified as public abstract, meaning that all methods in the interface are abstract methods. As a result, one can see the difference between an interface and an abstract class, which is an extremely abstract interface.

Implementing the interface requires the Implements keyword, in the form

Class ClassName implements Interface1, Interface2,...
{
}

As a result, there is another difference between an interface and an abstract class, where a class can inherit only one abstract class, but may implement multiple interfaces. When a class implements an interface, all the abstract methods of the interface must be implemented (the exception is when the implementation class is an abstract class)

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


Four. Guidelines for the use of abstract classes and interfaces


Let's take a look at the most widespread examples of online circulation: doors and alarms. Doors and alarms have open () and close () two actions, at which point we can define this abstract concept through abstract classes and interfaces:

Public abstract class Door
{
public abstract Void open ();

public abstract Void open ();

}

public interface Door
{
public abstract Void open ();

public abstract void Close ();

}

So the question is, if we now need to add alarm alarm () to the door, then how? Refer to the following two ways of thinking first:
1) These three functions are placed in the abstract class, but so that all the subclass inherited from the abstract class has an alarm function, but in fact, the door does not necessarily have the alarm function;
2) These three functions are placed in the interface, but only the class that needs to implement the alarm function will also implement open () and close (), but in fact the implementation class does not necessarily have open () and close () two functions, such as fire alarm.

As can be seen here, open (), close () and alarm () in door are fundamentally different categories, and open () and close () belong to the intrinsic behavioral characteristics of the gate itself, while alarm () is an extension of the additional behavior, So the best way to do this is to design the alert individually as an interface that contains the alarm () behavior, and door is designed as an abstract class that contains both open () and close () behaviors. At this point the alarm gate can be designed to inherit the door class and implement the alarm interface for a class

public interface Alarm
{
public abstract void alarm ();
}

Public abstract class Door
{
public abstract Void open ();

public abstract void Close ();

}

public class Alarmdoor extends Door implements Alarm
{
public void Open () {}

public void Close () {}

public void Alarm () {}

}

From this we can conclude that:

1) If you want to have some methods and want some of them to have a default implementation, use an abstract class.
2) 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.
3) If the basic ability is changing, then you need to use abstract classes. Otherwise, once an interface needs to be modified, all implementations of that interface must be modified accordingly

Reference documents

Http://www.cnblogs.com/dolphin0520/p/3811437.html

Http://www.importnew.com/12399.html

The difference between abstract classes and interfaces in Java (reading notes)

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.