The difference between abstract classes and interfaces in Java_ Power node Java college sorting

Interface

Because Java does not support multiple inheritance, with interfaces, a class can only inherit one parent class, but can implement multiple interfaces, and the interface itself can inherit multiple interfaces.

2. The member variables in the interface are of public static final type by default. Initialization that must be displayed.

3. All methods in the interface are public abstract by default. Implicit declaration.

4. The interface has no constructor and cannot be instantiated.

5 interface cannot implement another interface, but can inherit multiple interfaces.

If a class implements an interface, it must implement all abstract methods in the interface, otherwise the class must be defined as an abstract class.

abstract class

If a class is declared abstract, it cannot generate objects and can only be inherited.

2 abstract methods must exist in abstract classes.

3 abstract classes can have general variables and general methods.

Subclasses inherit abstract classes and must implement the abstract methods in them, unless the subclass is an abstract class.

Differences between interfaces and abstract classes

1 interfaces can only contain abstract methods, and abstract classes can contain ordinary methods.

2. The interface can only define static constant attributes. Abstract classes can define either common attributes or static constant attributes.

3. The interface does not contain construction methods, and the abstract class can contain construction methods.

An abstract class cannot be instantiated, but it does not mean that it cannot have a constructor. An abstract class can have a constructor for inheritance class expansion

The above is the difference between abstract classes and interfaces in Java introduced by Xiaobian_ The power node is sorted out by Java college. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>