[Java] "Java Programming Basics Tutorial" Chapter V learning

5.1 abstract class

Java language, some methods of the parent class does not include any logic, and need to be overridden by subclasses. In this case, when using the abstract keyword to modify a class, the class is called abstract class, when using the abstract keyword to modify a method, this method is called abstract methods.

Abstract method declarations only, without implementing; abstract class that can not be instantiated, indirectly must be achieved by a subclass of abstract class; abstract class not necessarily include an abstract method, if the class contains abstract methods, the class must be defined abstract.

An abstract class may contain a non-abstract methods. Inherited abstract class must implement the abstract methods abstract class, otherwise, must also be defined as an abstract class. Even if no abstract methods abstract class, but also we need to create an instance of the class after his son inherited.

5.2 Interface

The interface is an abstract class, comprising only the definitions of constants and methods, without the variables and methods to achieve, and which methods are abstract. Its usefulness is reflected in the following aspects:

1. Definition Interface

(1) By the same interface behavior is not related classes, regardless of the relationship between these classes

(2) The method via a plurality of interfaces need to achieve a specified

(3) through an interface object interface to understand, without knowing the corresponding class object

Some statements are not allowed to use the modifier in the member declaration interfaces, such as not using a transient, volatile or synchronized in a member declaration interface. Similarly, you can not use private and protected modifiers when declaring an interface member

2. implement interface

In the class declaration implements clause used to represent a class implements an interface, the interface constants defined in a class body may be used, and all the abstract methods defined in the interface must be implemented. A class can implement multiple interfaces, with comma separated implements clause.

3. the difference between abstract class interface, class

(1). The method can not have non-abstract class interface, the abstract class can have

(2). A class can implement multiple interfaces, but only one parent

(3) The interface can also be inherited, with the classes, interfaces the plurality of interfaces can inherit

(4) can be achieved independent of the class the same interface

5.3 package

Package is a collection of related classes and interfaces of a set.

Organization and management package not only makes it easier for the class, the class also eliminates potential conflicts of different groups of class name

Guess you like

Origin www.cnblogs.com/daijux/p/12016966.html