Home > Java > JavaBase > The difference between inheritance and implementation in java

The difference between inheritance and implementation in java

王林
Release: 2019-11-18 17:05:22
Original
5706 people have browsed it

The difference between inheritance and implementation in java

Inheritance: If a certain part of multiple classes has the same function, then you can abstract a class and put the same parts of them into the parent class so that they all inherit this class.

Implementation: If the processing goals of multiple classes are the same, but the processing methods are different, then define an interface, which is a standard, and let them implement this interface and each implement their own specific processing. methods to deal with that goal.

The difference between the two:

1. Different modifications

Different modifier modifications; implementation: implements, inheritance: extends;

2 , The number is different

Java only supports multiple inheritance of "interface" and does not support multiple inheritance of "class"; inheritance has a single root in Java, and subclasses can only inherit one parent class.

Summary: single inheritance, multiple implementations.

3. Different attributes

Only global constants (static final) and methods without implementation can be defined in the interface; while in inheritance Attribute methods, variables, constants, etc. can be defined in it.

4. Call different

When an interface is implemented by a class, the abstract method in the interface must be implemented in the class; while inheritance wants If you call that method, call that method.

Summary:

The interface is: a description of the function (method/behavior). The meaning conveyed by the interface is: what can you do if you have a certain function? For example: Serializable represents serializable.

Inheritance is: what is a kind of what, inheritance conveys the meaning: is-a, for example: cat is an animal, and cat is a subclass of animal.

Recommended tutorial: Java tutorial

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template