Abstract classes, interfaces, and packages

Source: Internet
Author: User
Tags inheritance
5.5
Abstract classes, interfaces, and packages
5.5.1
Abstract class
Suppose we're going to write a calculation of the area and perimeter of a rectangle, triangle, and circle.
program, if you are programmed in the way we learned before, we must define four classes: Circle
class, triangle class, rectangular class, and public classes that use the first three classes, which have no
There are inheritance relationships, as shown in Figure 5.4. After the program has been written, although it can be executed, the program
The overall structure of the three classes of many common attributes and operations in the program
is not well exploited, resulting in repetitive coding, which reduces the development effectiveness of the program
Rate and increase the chance of error.
5th chapter message, Inheritance and polymorphism
Figure 5.4 Several classes that have the same characteristics but are independent of each other
Circle class
Center coordinates
Radius
Calculate area
Calculate perimeter
Triangle class
Bottom length
High
Calculate area
Calculate perimeter
Rectangular class
Long
Wide
Calculate area
Calculate perimeter
5th chapter message, Inheritance and polymorphism
With a careful analysis of the three classes in the above example, you can see that all three classes are counted
Calculate area and perimeter, although the formula is different but the target is the same. Therefore, we can
These three classes abstract out a parent class that defines circles, triangles, and rectangles in the parent class three
A class is a common data member and member method. To calculate the area and perimeter of the member side
The Dharma name is given in the parent class, and the concrete calculation formula is implemented in the subclass.
As shown in Figure 5.5. In this way, we know from the parent class what the subclass is going to do
Tasks, and these methods can also be applied to the solution of parallelogram, trapezoid, etc.
The perimeter and area of the graph. This structure is the concept of abstract classes.
5th chapter message, Inheritance and polymorphism
Figure 5.5 abstract classes and their applications
Circle class
Center coordinates
Radius
Triangle class
Bottom length
High
Calculate area
Calculate perimeter
Long
Wide
Abstract class
Trapezoidal class
Calculate area
Calculate perimeter
Abstract methods
Calculate area
Calculate perimeter
Rectangular class
Calculate area
Calculate perimeter
Upper and Lower Bottom
Long height
5th chapter message, Inheritance and polymorphism
Abstract classes (abstract class) are used in Java programs to realize the abstraction of nature
Read. The function of an abstract class is to organize many related classes together to provide a public
A common class, that is, an abstract class, and a specific class that is organized by it as its
The subclass is derived from it. Abstract classes depict the characteristics of public behavior and inherit
The mechanism is passed to its derived class. The method defined in an abstract class is called an abstract method,
These methods have only the declaration of the method header, and a semicolon replaces the method body's definite
Meaning, which defines only the interface form of a member method, without concrete action. Only derived
class to a redefinition of an abstract member method to really implement the actions associated with that derived class.
After each subclass inherits the abstract method of the parent class, separate statements and sides
The legal body to redefine it, form several names the same, return the same value, parameter columns
The table is the same, the purpose is consistent but the specific method of achieving a certain difference. In an abstract class
The purpose of defining an abstract method is to implement an interface in which all subclasses are rendered externally
A method of the same name.
5th chapter message, Inheritance and polymorphism
An abstract class is a collection of public properties for all of its subclasses, which contains one or
A class of multiple abstract methods. One of the great advantages of using abstract classes is that you can make full use of
These public properties to improve the efficiency of your development and maintenance programs. For abstract classes and pumping
The limitations of the image method are as follows:
(1) with abstract
A class decorated with modifiers is called an abstract class. Usually used
The member method decorated by the abstract modifier is called an abstract method.
(2) An abstract class can have 0 or more abstract methods, or it can contain a non-smoked
The method of the elephant.
5th chapter message, Inheritance and polymorphism
(3) There can be no abstract method in an abstract class, but a class with an abstract method must
Must be an abstract class.
(4) For abstract methods, only their method names and their classes are specified in the abstract class
Type, without writing its implementation code.
(5) Abstract classes can derive subclasses that must be implemented in subclasses derived from abstract classes
All abstract methods defined in the abstract class.
(6) An abstract class cannot create an object, and the work of creating the object is derived from the abstract class
Subclass to implement.
5th chapter message, Inheritance and polymorphism
(7) If an abstract method with the same name already exists in the parent class, then the subclass cannot
has an abstract method with the same name.
(8) Abstract cannot modify the same class as final.
(9) Abstract cannot be modified with private, static,final or native.
A method.
(a) An abstract class cannot have a private data member or member method.
5th chapter message, Inheritance and polymorphism
"Sample program C5_18.java" abstract class application.
Import java.awt.*;
Import Java.applet. *;
Abstract class shapes//define an abstraction type shapes
{
public int x,y; X, y is the coordinates of the drawing
public int width,height;
public Shapes (int x,

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.