Home  >  Article  >  Java  >  JAVA summarizes abstract classes and interfaces

JAVA summarizes abstract classes and interfaces

黄舟
黄舟Original
2017-02-20 09:59:431318browse

1. Abstract class

a) A subclass of an abstract class must override all abstractions of the parent class Method

i. If you don’t want to override it, unless the subclass itself is also an abstract class;

b) Do abstract classes allow constructors?

i. Allow! (Check after class), explanation: The constructor of the drawing class has another clever application: it is to implement the code that the subclass must execute inside it.

2. MyeclipseUsage details

a) at MyEclipse A triangle will be displayed on the left;

i. If it is a solid triangle, it means that this method is heavy Write a regular method of the parent class;

ii. If it is a hollow triangle, it means that this method is an overridden abstract method of the parent class;

3. Polymorphism

a) Conditions for achieving polymorphism:

i. Must have inheritance;

ii. Must have overriding;

iii. The parent class reference points to the object of the subclass;

b) The role of polymorphism:

i. Eliminate the coupling between classes; (check after class)

ii. Polymorphism reflects the scalability and flexibility of the program;

c) Two methods to achieve polymorphism:

i. Use the abstract parent class as the formal parameter of the method;

ii. Use the abstract parent class as the return type of the method;

4. finalKeyword: final meaning

a) Modified class means that this class does not allow subclasses;

b) Modified method rules Indicates that this method does not allow overriding;

c) Modifying a variable means that the variable has become a constant;

5. The focus of this section - interface ! 《Key Master》

a) It is a special abstract class;

i. The reason for it is: some special methods are not suitable to be abstracted into the parent class;

The reason is that not many subclasses need it Have this kind of behavior;

In this case: what we need to take care of is the commonality while taking into account the characteristics;

To solve this This contradictory thing is called an interface!

b) The essence of the interface: represents a capability; a standard; a description;

i . The interface is the classification management of classes;

It only stipulates what to do; the specific how to do it is subdivided into the subclasses themselves;

c) A class can only inherit one parent class but can implement multiple interfaces;

That is to say, the interface can implement multiple disguised forms. Inheritance;

d) Basic syntax of interface:

i. There are only Public abstract methods are allowed;

ii. Only public static final variables (constants) are allowed in the interface ) (Self-certification after class)

Six, The interface contains the methods to be implemented by the class

7. Similarities between interfaces and classes:

a) An interface can have multiple methods.

b) The interface file is saved in a file ending with .java, and the file name uses the interface name.

c) The bytecode file of the interface is saved in a file ending with .class.

d) The bytecode file corresponding to the interface must be in the directory structure matching the package name.

8. The difference between interface and class:

a) Interface cannot be used Instantiate the object.

b) The interface has no constructor method.

c) All methods in the interface must be abstract methods.

d) The interface cannot contain member variables, except static and finalvariable.

e) The interface is not inherited by the class, but must be implemented by the class.

f) The interface supports multiple inheritance.

The above is the summary of abstract classes and interfaces in JAVA. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn