Java basics: 38th abstract classes and abstract methods

Source: Internet
Author: User

This section describes the following issues: what is the relationship between abstract classes, abstract classes, abstract methods, abstract methods, and abstract classes.

1. What is an abstract class?

Abstract classes are actually further abstractions of classes.

From the previous introduction, we know that there are many abstractions in object-oriented analysis and design, and the objects in the software system are abstracted from objects in the real world (retaining the main attributes and behaviors, remove secondary attributes and behaviors), Abstract The classes in the software system from the objects in the software system (remove attribute values and change specific objects into abstract concepts ), the abstract class we want to introduce is the abstract result of multiple classes. The abstract class describes the common features and behaviors of multiple specific classes, which is similar to the parent class we mentioned earlier, the difference is that abstract classes cannot have specific objects, and abstract classes can have methods that do not need to be implemented.

A typical example is transportation. We know that airplanes, ships, cars, and trains are all transportation tools. They can all take passengers from one place to another, however, different means of transportation run differently. When a car goes along the road and a train goes along the railway line, the plane needs to take off, fly, and then land. There are certain routes. It is because of these common features that a class like transportation is abstracted.

We can say that specific aircraft, ships, vehicles, and train objects, but there are no transport objects. At this time, transport is abstract.

2. How to define abstract classes

Use abstract keywords to define abstract classes, for example:

Public abstract class {

...

}

If a class is an abstract class, the class cannot have specific objects.

If a class does not have a specific object, what is the use of this class? This class can be used to derive child classes, which can be abstract classes or non-abstract classes. Therefore, abstract classes usually exist as parent classes.

3. Abstract METHODS

In the previous introduction, we know that methods include the following components: Method modifier, type of return value, method name, method parameter list, and method body. For example:

Public int getparenta (){
Return super.;
}

If a method only defines the method, but does not implement the method, this method is an abstract method.

For example:

Public int getparenta ();

Abstract method: an object has such a method, but its implementation is unclear.

For example, in the above example, all kinds of transportation tools can take passengers from one place to another. Therefore, transportation tools should have a method of moving, but it is unclear how to move them, therefore, to define this method in the class of transportation tools, it can only be abstract. This means that there should be a method (MOVE) for transportation, but how to implement it depends on the specific subclass.

Another typical example is ry. In a software system, You need to draw various types of graphics. Each type of graphics is a specific class, and graphics are used as abstract classes. To display various images in the system, each type of graphics should have a painting method. However, different graphics have different painting methods. For example, a triangle must have three points, and a circle must have a center and a radius, two points must be given for a straight line. Therefore, only one painting method can be declared in the abstract graph class. The specific implementation is left to the subclass.

4. How to define abstract methods

Abstract methods are defined using keywords abstract. For example, the drawing method can be defined:

Public abstract void print ();

In a class, if a method only gives a declaration without specific implementation, abstract should be used to define it as an abstract method.

5. Abstract methods and abstract classes

If an abstract method exists in a class, the class must be defined as an abstract class. For example, the preceding graphic class:

Public abstract class graphics {

...

Public abstract void print ();

}

Sub-classes can implement specific painting methods, such as triangles:

Public class triangle extends graphics {

Private point [] P; // describes the three points of a triangle.

Public abstract void print (){

... // Draw a picture based on three points

}

}

Remember: classes with abstract methods must be abstract classes.

Must abstract classes have abstract methods? Not necessarily. abstract classes can have no abstract methods.

The abstract class must not have instances.CodeIs incorrect:

Graphics G = new graphics ();

6. Role of abstract classes

Abstract classes are used to derive new subclasses. When a subclass inherits an abstract class, all abstract methods must be implemented unless the subclass is still an abstract class.

The subclass of an abstract class can be an abstract class.

Next we will introduce a major feature of object orientation-polymorphism.

Last time: 37th lecture on the inheritance and coverage of member Methods

Next time: 39th Polymorphism

Li xucheng csdn blog: Http://blog.csdn.net/javaeeteacher Invite you as a friend:Http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

 

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.