Inheritance is a cornerstone of Java object-oriented programming technology because it allows the creation of hierarchical classes. Inheritance means that a subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class.
Java interface is a series of method declarations and a collection of method characteristics. An interface only has method characteristics but no method implementation, so these methods can be implemented by different classes in different places, and these implementations Can have different behaviors (functions).
The difference between java inheritance and interface:
Difference 1:
Different modifiers modify (interface), (extends)
Difference 2:
In object-oriented programming, multiple inheritance is possible! However, only multiple inheritance of interfaces is supported, and multiple inheritance of 'inheritance' is not supported. Inheritance has single root in Java, and subclasses can only inherit one parent class.
Difference 3:
Only global constants and abstract methods can be defined in the interface, while in inheritance, attribute methods, variables, constants, etc. can be defined...
Difference 4:
When an interface is implemented by a class, the abstract method in the interface must be implemented in the class. If you want to call that method through inheritance, you can call that method without any pressure.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of The difference between java inheritance and interface. For more information, please follow other related articles on the PHP Chinese website!