The differences between interfaces and inheritance in Java

Source: Internet
Author: User

Actual concept differences:
Difference 1:
different modifier modifiers (interface), (extends)
Difference 2:
You can have multiple inheritance in object-oriented programming! However, multiple inheritance of interfaces is supported, and inheritance is not supported.
Whereas inheritance has a single root in Java, subclasses can inherit only one parent class
Difference 3:
Only global constants, and abstract methods, can be defined in an interface
In inheritance, you can define attribute methods, variables, constants, etc...
Difference 4:
When an interface is implemented by a class, the abstract method in the interface must be implemented in the class
And the inheritance wants to invoke that method and call that method without pressure.

The interface is: Description of the function inheritance is: What is a kind of what

Always Reporter: You can have more than one Father (interface), but only have a father (inherit)

Example:

If the dog's owner just wants the dog to climb the lower tree, but does not want it to inherit the tail can hang upside down in the tree, like the monkey can Feiyanzoubi, lest the master can't control it.

Then the owner of the dog must not want a dog inherited by a monkey.

Design patterns are more emphasis on interface-oriented. Monkeys have two interfaces, one is a tree climb, the other is a tail upside down. I just need my dog to climb the tree, but don't hang it upside down, so I'll just have to get my dog to climb the tree. At the same time will not bring like inherited monkeys to bring the tail upside down side effects. This is the benefit of the interface.

46353251

The differences between interfaces and inheritance in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.