Thinkinginjava Learning Note 08_ interface

Source: Internet
Author: User
Tags what interface

Abstract classes and abstract methods

Abstract methods are methods that have no concrete implementation, only declarations of methods and no method bodies; An abstract method is defined using the abstract keyword; A class that contains an abstract method becomes an abstract class, and if a class contains an abstract method, you must use abstract to qualify the class as an abstract class Abstract classes cannot instantiate objects, and subclasses of abstract classes must provide method definitions for all abstract methods, otherwise they are still abstract classes and must be qualified with abstract;

Interface

An interface is a completely abstract class that does not provide any specific implementation, but provides a concrete method form (method name, parameter list, return value), which is used to establish the Protocol between class and Class (protocol) because the class is through interface and external communication. , use interface instead of class to define an interface, in the interface, all methods are public, even if not add public, will automatically become public, otherwise this will limit the meaning of the interface;

interfaces, in addition to abstract methods, can also contain fields, but these fields will implicitly be static and final (constants, in uppercase letters) and can not be null final, can be initialized by constant expressions These fields are not part of the interface, but are stored in the interface's static storage area;

Having a class follow a specific (or group) interface requires the use of the Implements keyword to indicate what interface the class uses and how the class will implement the methods in the interface;

As in the example code, wind, percussion, stringed all use the Instrument interface, the table name four classes (interfaces) have the same behavior, derived from wind two sub-classes: Brass, woodwind, these two subclasses can also be transformed upward, Use this interface;

Fully decoupled

The meaning of an interface relative to inheritance is that if the method operates on a class, then the method can only use the class and its subclasses, otherwise it will be an error, and by means of a method operation an interface, there is no such restriction, as long as the class satisfies the interface can be called;

Create a method that can behave differently depending on the parameter object being passed, called the policy design pattern;

Publicclass Apply {

?? Public Static void process (Processor p, Object s) {

? ? ? ? println ("Using Processor" + p. Name ());

? ? ? ? println (P. Process (s));

? ? }

}

?

The Apply.process () method receives a Processor object and applies it to the object, and according to the implementation of the different processor interface (policy), the method can behave differently to object;

When processor is a class, the Apply.process () method is tightly coupled to the processor and can only receive objects of that class or its subclasses, and when a different class with the same interface uses the method, reuse is forbidden, and when processor is an interface , the coupling will be loose, as long as the object of the same interface class can be received;

If you already have a class filter that you want to change to the class of the processor interface, you need to use the adapter:

class Filteradapter implements processor{

? ? Filter filter;

? ? Filteradapter (filter filter) {

???? this. Filter = filter;

? ? }

?? Public String name () {

???? return filter. Name ();

? ? }

?? Public Object process (object input) {

???? return filter. Process ((waveform)input);

? ? }

}

?

The class uses the processor interface, and accepts a filter object, using the proxy to match the filter object like the processor interface;

See: Sample code, where dependent parts are found in the same path under the various class files;

Multiple inheritance

In the sample code, hero inherits the base class Actioncharacter and inherits three interfaces: Canswim, Canfight, canfly; At this point, the concrete base class must be placed in front, and the interface is placed behind and separated by commas;

In Adventure, four methods are implemented, and different interfaces and classes are passed in, and Hero objects can be passed to any of these interfaces or classes;

Interfaces can be extended by inheriting other interfaces, extended using extends, and separated by commas if multiple interfaces are inherited (extends for a class only applies to a single class);

When using multiple inheritance, you should avoid the existence of the same method names in many different interfaces, which can cause confusion in code readability;

With multiple inheritance, the adapter can be expanded to include interfaces on top of any existing classes, adapting existing classes to a method;

Interfaces and factories

As in the example code, the factory method is used to avoid invoking a particular construction method when using an object of a service class, but instead to use a factory method of a particular interface and implement a call to the constructor in it, as long as the interface is implemented;

The discussion of factory methods is also mentioned in effective Java, there is only one subjective understanding, need to deepen in use;

Thinkinginjava Learning Note 08_ 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.