Home  >  Article  >  Java  >  The difference between interface and inheritance in java

The difference between interface and inheritance in java

尚
Original
2019-12-30 15:08:428483browse

The difference between interface and inheritance in java

Interface (English: Interface) is an abstract type in the JAVA programming language and is a collection of abstract methods. Interfaces are usually declared with interface. A class inherits the abstract methods of the interface by inheriting the interface.

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.

Recommended: java video tutorial

The difference between java interface and inheritance:

1. Different modifier modifications ( interface), (extends)

2. Multiple inheritance is possible in object-oriented programming! However, only multiple inheritance of interfaces is supported, and multiple inheritance of 'inheritance' is not supported. Inheritance has single root in java. Subclasses can only inherit from one parent class.
3. Only global constants and abstract methods can be defined in interfaces, while attribute methods, variables, constants, etc. can be defined in inheritance.

4. When an interface is implemented by a class, the abstract method in the interface must be implemented in the class, and if the inheritance wants to call that method, call that method.

5. Multiple inheritance is not supported in JAVA, but it can be implemented using interfaces, so you need to use implements

6. Inheritance can only inherit one class, but implements can implement multiple interfaces. , just separate them with commas.

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of The difference between interface and inheritance in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn