What is the adaptation class for java-7.6 adaptation design patterns? Or the adaptation interface?

Source: Internet
Author: User

What is the adaptation class for java-7.6 adaptation design patterns? Or the adaptation interface?

In this chapter, let's discuss the adaptation issue?

To put it bluntly, you can use any object to call a method, and this object only needs to comply with certain protocols.

The implementation of this protocol has two methods: inheritance and interface, but interfaces are generally used, because their code reusability is better than inheritance, and inheritance itself also contains many implementations of the parent class, cause unnecessary confusion. In addition, we can implement multiple interfaces (similar to multiple inheritance) through interfaces)

The following code compares the two groups:

 

package com.ray.ch07;class father {private int id = 0;private void sleep() {}public int run(int speed) {return speed;}}public class Test extends father {@Overridepublic int run(int speed) {// TODO Auto-generated method stubreturn super.run(speed);}}

Through inheritance, it is generally like the above Code. Test inherits from father, and the test object can only be transformed to a father class. If multiple inheritance is required, the hierarchy of inheritance may increase suddenly.

 

 

Let's take a look at the interface:

 

package com.ray.ch07;interface Son {void sleep();int run(int speed);}interface Mother {void eat();}public class Test implements Son, Mother {@Overridepublic void eat() {// TODO Auto-generated method stub}@Overridepublic void sleep() {// TODO Auto-generated method stub}@Overridepublic int run(int speed) {// TODO Auto-generated method stubreturn 0;}public static void main(String[] args) {Mother mother = new Test();mother.eat();Son son = new Test();son.sleep();son.run(2);}}

In the above Code, Test can easily be transformed to Son and Mother classes. From the perspective of polymorphism, using interfaces to adapt to code greatly increases reusability and code is more flexible.

 

 

Summary: This chapter mainly discusses which one is used in the adaptation implementation. We recommend that you use the interface, because it brings more flexibility and code reusability.

 

 

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.