Ii. Java Object-oriented (14) _ Abstract classes and abstract methods

Source: Internet
Author: User

2018-05-06

abstract classes and abstract methods

I. Use of abstraction

When certain methods of the parent class are not deterministic, the abstract keyword can be used to decorate the method [abstraction], and abstract to decorate the class [abstract class].

We all know that the parent class is to extract the properties and methods that the subclass has in common, some of which have already been explicitly implemented, some of which cannot be determined, then we can define them as abstractions and reuse them in the later days to materialize. In this way, the abstract class is born.

For example, the "animal" parent class is defined, where the "animal name" and "Animal age" properties have been clarified, but the "animal called" method is not clear, and "animal call" can be defined as an abstract method.

  Therefore, abstract classes are designed to extract the same but indeterminate things for later reuse. The purpose of defining abstract classes is to implement abstract classes in subclasses.

Ii. abstract Methods

  The abstract method in Java is a method of abstracting, which declares only the returned data type, method name, and required parameters, without the method body, which means that the abstract method needs to be declared without implementation.

  Characteristics:

1, using the abstract modification, there is no method body, subclasses should overwrite (implement) all the abstract methods of the parent class.

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

The subclass cannot access the private method of the parent class, and the final decorated method cannot be inherited, static is the class level, and this is called hiding instead of overwriting.

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

Third, abstract class

  When a method is an abstract method, it means that the method should be overridden by the method of the class, otherwise the method of its subclass is still abstract, and the subclass inherits the parent class, has an abstract method, and therefore it is also an abstract class, that is, the declaration of abstraction.

Abstract class cannot instantiate an object with new, and the abstract method only allows the declaration to not be implemented. If a class contains an abstract method, then the class must be decorated with abstract, although the abstract class can also have no abstract method.

  Characteristics:

1. Abstract classes cannot instantiate objects with new.

2. Abstrace methods are not necessarily included in abstract classes. That is, there can be no abstract method in the abstraction. Once the class contains an abstract method, that class must be declared as an abstract class.

3, if the subclass does not implement (overwrite) all the abstract methods of the parent class, then the subclass is also an abstract class (because the method of the subclass is an abstract method, and the class of the abstract method is necessarily an abstract class).

4. The constructor method of an abstract class cannot be declared as private, otherwise it cannot have subclasses (the parent class method is called before the subclass object is created).

5. Abstract classes cannot be declared final because they must have subclasses, and abstract methods can be implemented.

6, abstract class is incomplete, must have sub-class, the function can be realized.

Abstract methods can be absent from an abstract class to prevent the creation of objects from the outside world.

  

Iv. the difference between abstract and common methods

The common class has members (fields, methods, constructors), and abstract classes can exist.

The difference is that abstract classes cannot create objects, and abstract classes can exist in abstract ways.

  

Ii. Java Object-oriented (14) _ Abstract classes and abstract methods

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.