"Java and Mode" learning notes interface and abstract class

zhaozj2021-02-16  49

"Java and Mode" learning notes interface and abstract class

interface:

A Java interface is a collection of method features, of course, from specific methods, but they are generally derived from some ways to appear in the system. An interface only features features, and there is no way to implement, so these methods can have a completely different behavior when implemented in different places. In the Java language, the Java interface can also define the constant of the public.

When using the interface, you need to indicate the interface itself, and the class that implements this interface. A class implements an interface, which is called an interface inheritance; and a class is a subclass of another class, which is called implementation inheritance. Interface inheritance is different from the rules of the inheritance, and a class can only have a super-class, but several interfaces can be implemented at the same time.

The interface makes the insertability possible. Any interface can be implemented in any class in a class level structure, which affects all subclasses of such classes, but will not affect any superclasses in this class. Such a method specified in this interface will have to be implemented, and its subclass can automatically inherit these methods from this class, and of course you can choose to replace all of these methods, or some of them. At this time, these subclasses have an insertable.

If an association is not for a specific class, but for an interface, any class that implements this interface can meet the requirements. This will dynamically convert this association from a specific class to another, and the only condition that does this is that they all implement an interface.

Similarly, an object inevitably needs to call other objects. This call is not necessarily a specific class, but can be an interface. In this way, any specific class that implements this interface can be called by the current object; and the current object is called which specific class of the specific class, it is fully dynamically determined.

Interfaces are often used to declare a new type, generally as a starting point for a type level structure.

Abstract class:

The abstract class only provides a type of partial implementation. Abstract classes can have example variables, and one or more constructors. Abstract classes can have an abstract method and a specific method. It provides a starting point of inheritance and cannot be instantiated. Therefore, the abstract class must be used to inherit.

From an abstract class to multiple specific classes inheritance, the common code should be moved to the abstraction class, while the moving direction of the data is from the abstraction class to the specific class. The data of an object will occupy resources regardless of whether it is used, so the data should be as low as low ends of the specific class or level structure.

Inheritance relationship should be used when the following conditions are met:

1 Subclass is a special type of superclass, not a super class. The HAS-A relationship should be described using a polymerization relationship, and only IS-A relationships meet inheritance relationships.

2 There will never be a subclass of the subcategory to another class. If the designer is not very sure that a class will become a subclass of another class in the future, this class should not be designed to the current superclavab.

The three subclasses have the responsibility of expanding superclars, rather than having to replace or cancel the superclass. If the subclass needs to set a large amount of superior behavior, then this subclass should not be the subclass of this superclass.

4 Only inheritance can only be used when there is a meaningfulness of the classification, do not inherit from the tool class.

转载请注明原文地址:https://www.9cbs.com/read-25080.html

New Post(0)