Java interfaces and abstract classes

The difference between Java abstract class and interface
http://www.importnew.com/12399.html

In-depth understanding of Java interfaces and abstract classeshttp
://www.cnblogs.com/dolphin0520/p/3811437.html

Java abstract classes and interfaces
http://www.cnblogs.com/qifengshi/p/5682255.html

java code geeks
https://www.javacodegeeks.com


 

abstract class vs interface

parameter abstract class interface
Default method implementation It can have default method implementation Interfaces are completely abstract. it doesn't have an implementation of the method at all
accomplish Subclasses use the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide implementations of all the methods declared in the abstract class. Subclasses use the keyword implements to implement the interface. It needs to provide implementations of all declared methods in the interface
Constructor Abstract classes can have constructors An interface cannot have a constructor
Differences from normal Java classes It is no different from a normal Java class except that you cannot instantiate an abstract class Interfaces are completely different types
access modifier Abstract methods can have public , protected and default modifiers The default modifier for interface methods is public . You cannot use other modifiers.
main method Abstract method can have main method and we can run it The interface doesn't have a main method, so we can't run it.
multiple inheritance Abstract methods can inherit from a class and implement multiple interfaces An interface can only inherit from one or more other interfaces
speed it is faster than the interface The interface is slightly slower because it takes time to find the methods to implement in the class.
add new method If you add new methods to an abstract class, you can provide it with default implementations. So you don't need to change your current code. If you add methods to an interface, then 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 abstract classes.
  • If you want to achieve multiple inheritance then you have to use interfaces. Since Java does not support multiple inheritance , subclasses cannot inherit multiple classes, but can implement multiple interfaces. So you can use interfaces to solve it.
  • If the basic functionality is constantly changing, then you need to use abstract classes. If you keep changing the basic functionality and using an interface, then you need to change all classes that implement the interface.

Default and static methods in Java 8

Oracle has begun to try to introduce default methods and static methods into interfaces to reduce the difference between abstract classes and interfaces. Now, we can provide a method with a default implementation of the interface without forcing subclasses to implement it. I will discuss this in the next blog post.

 


The inheritance of the interface itself, (inheritance of the interface) the
interface contains other interfaces, (the interface contains the interface)
the use of the interface to implement the callback and so on (interface callback)


Difference between abstract class and interface

1. Differences at the grammatical level

  1) Abstract classes can provide implementation details of member methods, while only public abstract methods exist in interfaces;

  2) Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type;

  3) An interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

  4) A class can only inherit one abstract class, but a class can implement multiple interfaces.

2. Differences at the design level

  1) An abstract class is an abstraction of a thing, that is, an abstraction of a class, while an interface is an abstraction of behavior. An abstract class abstracts the whole class, including attributes and behaviors, but an interface abstracts a part of the class (behavior). To give a simple example, airplanes and birds are different things, but they all have one thing in common, that is, they both fly. Then when designing, you can design the aircraft as a class Airplane, and design the bird as a class Bird, but you cannot design the feature of flight as a class, so it is only a behavioral feature, not an abstract description of a class of things . At this time, the flight can be designed as an interface Fly, including the method fly( ), and then Airplane and Bird respectively implement the Fly interface according to their own needs. Then there are different types of aircraft, such as fighter jets, civil aircraft, etc., can directly inherit Airplane, and the same is true for birds, different types of birds can directly inherit the Bird class. It can be seen from this that inheritance is a "is or not" relationship, while interface implementation is a "is there or not" relationship. If a class inherits an abstract class, the subclass must be the type of abstract class, and the interface implementation depends on whether or not it has the relationship, such as whether the bird can fly (or whether it has the feature of flying), can fly You can implement this interface, but if you can't fly, you can't implement this interface.

  2) The design level is different. As the parent class of many subclasses, the abstract class is a template design. The interface is a code of behavior, it is a radial design. What is template design? The simplest example, everyone has used the templates in ppt. If template A is used to design ppt B and ppt C, the common part of ppt B and ppt C is template A. If their common parts need to be changed, only need to be changed Template A is fine, no need to re-modify ppt B and ppt C. In the radial design, for example, an elevator is equipped with some kind of alarm. Once the alarms are to be updated, they must be updated. That is to say, for an abstract class, if you need to add a new method, you can directly add a concrete implementation in the abstract class, and the subclass can not change it; but not for the interface, if the interface is changed, all the implementations of this interface Classes must be modified accordingly.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252589&siteId=291194637