Home  >  Article  >  Java  >  Detailed explanation of the difference between implement and extends in Java

Detailed explanation of the difference between implement and extends in Java

黄舟
黄舟Original
2017-08-22 10:16:362533browse

This article mainly introduces relevant information that explains the difference between implement and extends in JAVA. extends is the inheritance interface, and implement is the keyword for a class to implement an interface. Friends who need it can refer to it

Detailed explanation of the difference between implement and extends in JAVA

extends inherits from the parent class. As long as the class is not declared as final or the class is defined as abstract, it can be inherited. Multiple inheritance is not supported in Java. But it can be implemented using interfaces, so you need to use implements. Inheritance can only inherit one class, but implements can implement multiple interfaces, just separate them with commas. For example, class A extends B implements C,D,E

implements is a keyword used by a class to implement an interface. It is used to implement the abstract methods defined in the interface.

There are a few more points to note:

(1) Interfaces can be implemented by multiple implementations, abstract classes can only be extended by a single one

(2) Interfaces only have definitions, abstract classes Can be defined and implemented

 (3) The default field definition of the interface is: public static final, the default abstract class field is "friendly" (visible in this package)

 For example: people is an interface , it has the method of saying. public interface people(){ public void say();} But the interface has no method body. The method body can only be implemented through a specific class. For example, the Chinese class implements the people interface.


 public class chinese implements people
{ public void say() 
{System.out.println("你好!");
}
}

In Java, implements means that the subclass inherits the parent class. For example, class A inherits class B and is written as class A implements B{}

The difference between Extends

Extends, you can implement the parent class, or you can call the parent class to initialize this.parent(). And it will overwrite variables or functions defined by the parent class. The advantage of this is that the architect can define the interface and let the engineer implement it. The development efficiency of the entire project is improved, but the development cost is greatly reduced.

Implementations, implement the parent class, and subclasses cannot override the methods or variables of the parent class. Even if the subclass defines the same variables or functions as the parent class, they will be replaced by the parent class.

The specific use of these two implementations depends on the actual situation of the project and the need to implement it. Implements cannot be modified. Only the defined interface needs to be implemented specifically, or it can be modified for good scalability. Use extends.

The above is the detailed content of Detailed explanation of the difference between implement and extends 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