Second, Java object-oriented (14) _ abstract classes and abstract methods

2018-05-06

 

abstract classes and abstract methods

 

The use of abstraction

  When some methods of the parent class are uncertain, you can use the abstract keyword to modify the method [abstract method], and use abstract to modify the class [abstract class].

  We all know that the parent class extracts the attributes and methods that the subclasses have in common. Some of these attributes and methods have been clearly implemented, and some have not yet been determined, so we can define them as abstract, in After the class is reused, it is concreted. In this way, the abstract class was born.

  For example, if the parent class of "Animal" is defined, the attributes of "Animal Name" and "Animal Age" have been specified, but the method of "Animal Name" is not specified. At this time, "Animal Name" can be defined as an abstract method.

  Therefore, the abstract class is to extract the same but uncertain things for later reuse. The purpose of defining an abstract class is to implement an abstract class in a subclass. 

 

2. Abstract Methods

  An abstract method in java is a method decorated with abstract. This method only declares the returned data type, method name and required parameters, without a method body, that is to say, an abstract method only needs to be declared and not implemented.

 

  Features:

  1. Use abstract modification, without method body, the subclass should override (implement) all abstract methods of the parent class.

  2. Abstract methods cannot use private, final, and static modifications.

    The subclass cannot access the private method of the parent class, the final modified method cannot be inherited, and static is the class level, which is not called coverage but hidden.

  3. An abstract method must be defined in an abstract class or interface (if a method exists in a class, the class must be modified with abstract).

 

Three, abstract class

  When a method is an abstract method, it means that the method should be overridden by the method of the subclass, otherwise the method of the subclass is still abstract. This subclass inherits the parent class and has abstract methods, so it is also an abstract class , which is declared as abstract.

  Abstract abstract classes cannot use new to instantiate objects, and abstract methods only allow declarations that cannot be implemented. If a class contains an abstract method, the class must be modified with abstract, and of course the abstract class can also have no abstract method.

 

  Features:

  1. An abstract class cannot instantiate an object with new.

  2. The abstract class does not necessarily contain the abstrace method. That is, there can be no abstract method in the abstract. Once a class contains an abstract method, that class must be declared abstract.

  3. If the subclass does not implement (override) all the abstract methods of the parent class, then the subclass is also an abstract class (because the method of the subclass is still an abstract method at this time, and the class where the abstract method is located must be an abstract class).

  4. The constructor of an abstract class cannot be declared as private, otherwise it cannot have subclasses (call the superclass method before creating the subclass object).

  5. An abstract class cannot be declared final, because there must be subclasses for abstract methods to be implemented.

  6. The abstract class is incomplete, and there must be subclasses before the function can be realized.

Abstract methods may not exist in abstract classes, which can prevent the outside world from creating objects.

  

Fourth, the difference between abstract classes and ordinary methods

  Members of ordinary classes (fields, methods, constructors), abstract classes can exist.

  The difference is: abstract classes cannot create objects, and abstract classes can have abstract methods.

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449669&siteId=291194637
Recommended