Java Programming Idea: 9th Chapter Interface

Source: Internet
Author: User

Interfaces and internal classes provide us with a more structured approach to separating interfaces from implementations.

9.1 Abstract classes and abstract methods

If our base class simply wants it to be inherited and thus derive the class through its interface operation, it does not make much sense to create the object itself, then we can design this base class as an abstract class. Mark a method that is not implemented in a base class as abstract. Doing so can make the class more abstract.

9.2 interface

The interface keyword makes the abstract concept more thorough, providing only the form, without any implementation. But it is not only a class that is extremely abstract, it also allows you to create a class that can be transformed up into multiple types to implement features similar to multiple inheritance.

Interfaces can also contain fields, but these fields are implicitly static final.

The method in the interface must be public (omit not to write), and the implementation method in the implementation class must also be public.

9.3 Complete Decoupling

as long as a method operates on a class rather than an interface, you can only use this class and its subclasses. If this method also wants to manipulate other types, an error occurs. The interface can greatly relax this limitation, allowing us to write code that is highly reusable. For example: There is a phone class, the interface is the player play (), there are different export classes inherit this phone class, a method to receive the phone object, call play method play music. However, this method can only receive phone objects and exported class objects. If I want to have the computer play music, because the computer does not belong to the mobile phone inheritance system, so this function cannot be implemented. But if we abstract the playback function into an interface, let the mobile phone implement this interface, the computer to implement this interface, then this method as long as the interface parameters can be received. Implement understanding coupling.

If the phone class is created by us, we can add implements directly, but what if it's a jar or someone else's, we can't modify it directly?

You can use the adapter mode, create an adapter class, implement an interface, combine the required objects, and invoke the corresponding method of the object in the method.

Multiple inheritance in 9.4Java

It is important to remember that the core reason for using an interface is to be able to move up to multiple base classes (and the resulting flexibility), and the second reason is the same as using abstract classes: Prevent clients from creating objects of that class.

9.5 Extending the interface through inheritance

Use the extends keyword to extend the method in the interface

9.6 Adapter Interface

It is useful to use the adapter mode to add new operand objects to an existing operator interface method.

Domains in the 9.7 interface

Implicitly static, final, which is used as a constant before an enum appears (Java SE5).

9.8 Nested interfaces

...

9.9 Interfaces and factories

Interfaces are a way to implement multiple inheritance, and the typical way to build a conforming interface object is the factory method design pattern.

Original way:

Method takes an interface as a parameter, void action (Service s) {...}

When called if (XXX) action (new SIMP1 ()), else if (XXX) action (new SIMP2 ()), else if .... Each call place needs to provide the implementation class object directly. If you need to modify/Add implementation class code, it will be redundant.

Factory mode:

void action (Servicefactory FAC) {Service s = fac.getservice ();

You just have to change it in factory.

9.10 Summary

Any abstraction should be the result of real demand. Classes should be preferred rather than interfaces. If the necessity of an interface becomes very clear, then refactoring. An interface is an important tool, but don't misuse it.

Java Programming Idea: 9th Chapter Interface

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.