Comparison between interfaces and inheritance Methods

Source: Internet
Author: User
When inheritance is used, it is mainly used to have the features required by the parent class without re-development and implementation details.

However, in many cases, a subclass does not need all the features of the parent class. It may only need some features. However, because of inheritance, all the features of the parent class are available, the required and unnecessary features are available at the same time. The child classes do not actually need to be used. Sometimes the features that are even avoided can be used at will, which is the side effect of inheritance. Especially in OO languages that allow multiple inheritance, it is easy to cause errors that are not easy to discover. Therefore, in the OO language, various regulations are created to restrict sub-classes from using certain methods in the parent class.

For example, if the dog owner only wants the dog to climb a relatively low tree, but does not want its tail to be upside down, it can fly like a monkey, so that the host cannot control it. Then the dog's master will certainly not need a dog inherited by a monkey.

The design mode emphasizes more interface-oriented. A monkey has two interfaces: climbing a tree and hanging its tail upside down. Now I only need my dog to climb the tree, but don't put its tail upside down, so I just need my dog to implement the tree crawling interface. At the same time, it will not bring the side effects of tail upside down like inheriting monkeys. This is the benefit of the interface.

OO technology has been developing for many years. An obvious trend is that inheritance is used less and less, and interfaces are used more and more widely. As a matter of fact, you only need to compare the earliest class libraries in JDK with the class libraries that have just been added recently, you can obviously feel the changes in the programming style in the OO technology field, from a large number of inheritance to interface-Oriented Programming with almost no need.

The interface is not an alternative inheritance. For example, if I want my animals to climb a tree, I don't need to know whether it is a dog or a monkey to climb a tree. I sent an animal that could climb the tree. This tree-crawling animal can be either a monkey or a dog. Isn't it flexible?

Dog (crawling trees, biting)
Monkey (crawling tree, tail upside down)

If I want to meet the requirements of tree crawling, I don't care whether it is a dog or not.

If I want to climb a tree or bite a person, I can select a dog or create an interface (climbing a tree or biting a person), and then let the dog implement the interface (climbing a tree or biting a person.

Because what I want is to implement the functions of my software, as long as I implement the functions I need, do I care whether it is a dog? Maybe a dog can. Maybe a dog can do it today. I don't care. I only need the interface.

Maybe I used a dog all the time to complete my tree-biting interface, but then I found another animal, such as a cat, which is more flexible than a dog in the tree-biting function, so I replaced the dog with a cat, and the Code does not need to be modified at all.

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.