Use of the Java abstract class

Source: Internet
Author: User
Tags function definition modifier

/*
When the same functionality appears in multiple classes, but the functional body is different,
This is available for upward extraction. At this point, only the function definition is extracted, not the functional body.

Abstract: Can not understand.

Features of abstract classes:
1, the abstract method must be in the abstract class.
2, both abstract methods and abstract classes must be decorated with the abstract keyword.
3, abstract classes can not create objects with new. Because it doesn't make sense to invoke an abstract method.
4, abstract methods in the abstract class to be used, the subclass must be replicated all the abstract methods, the child class object call.
If a subclass overrides only a subset of the abstract methods, then the subclass is an abstract class.

Java abstract class is determined : the Java abstract class embodies the idea of data abstraction, is a means to realize the polymorphism of the program. Referring to the limitations of abstract classes and abstract methods, we will introduce you to the use of abstract classes by using a small sample of Java abstract classes. I hope to be of some help to you.

Suppose we are going to write a program that calculates the area and perimeter of rectangles, triangles, and circles, and if programmed in the way previously learned, we must define four classes: round, triangular, rectangular, and public classes that use the first three classes, with no inheritance between them.

Although the program can be executed, but from the overall structure of the program, the three classes of many common properties and operations in the program is not well utilized, the need to write code repeatedly, reduce the development efficiency of the program, and increase the chance of error.

Careful analysis of the three classes in the example above, you can see that the three classes are to calculate the area and perimeter, although the formula is different but the target is the same. Therefore, we can abstract a parent class for these three classes, defining a common data member and a member method for the circle, Triangle, and rectangle three classes in the parent class. The name of the member method that calculates the area and perimeter is given in the parent class, and then the specific calculation formula is implemented in the sub-class.

In this way, we probably know the tasks to be done by the subclass through the parent class, and these methods can also be applied to solve the perimeter and area of parallelogram and trapezoidal graphs. This structure is the concept of abstract classes.
Java programs use abstract classes to implement abstract concepts in nature. The purpose of an abstract class is to organize many related classes together, to provide a common class, an abstract class, and the concrete classes that are organized by it will be derived from it as subclasses of that class. Abstract classes depict the characteristics of public behavior and are transmitted to its derived classes through inheritance mechanisms.

After each subclass inherits the abstract method of the parent class, it is redefined by different statements and method bodies, forming several names with the same name, returning the same values, and the same parameter list, but there are some differences in the implementation. The purpose of defining abstract methods in abstract classes is to implement an interface in which all subclasses render a method of the same name externally. An abstract class is a collection of public properties for all its subclasses and is a class that contains one or more abstract methods. One of the great advantages of using abstract classes is that you can take advantage of these common properties to improve the efficiency of your development and maintenance programs. The limitations for abstract classes and abstract methods are as follows:

(1) A class that is decorated with an abstract modifier is called an abstract class. A member method that is decorated with an abstract modifier is called an abstract method.
(2) There can be 0 or more abstract methods in an abstract class, or they can contain non-abstract methods.
(3) There can be no abstract method in an abstract class, but a class with an abstract method must be an abstract class.
(4) For an abstract method, only its method name and its type are specified in the abstract class, and the implementation code is not written.
(5) An abstract class can derive subclasses, and all abstract methods defined in an abstract class must be implemented in a subclass derived from an abstract class.
(6) Abstract classes cannot create objects, and the work of creating objects is implemented by subclasses derived from abstract classes.
(7) If an abstract method with the same name already exists in the parent class, then no abstract method of the same name can be found in the subclass.
(8) Abstract cannot decorate the same class with final.
(9) Abstract cannot be modified with private, static, final, or native side by side with the same method.
The Java Abstract class application sample program Test.java is as follows:

PublicAbstractClassShapes {Publicint x, y;Publicint width, height;PublicShapes (int x,int y,int width,int height) {this.x = x;This.y = y;This.width = width;This.height = height;}AbstractDouble Getarea ();AbstractDouble Getperimeter ();}PublicClassCircleExtendsShapes {PublicDouble R;PublicDoubleGetarea () {Return (R * R * Math.PI);}PublicDoubleGetperimeter () {Return (2 * Math.PI * r);}PublicCircle (int x,int y,int width,int Heigh) {Super (x, y, width, heigh); r = (Double) Width/2.0;}}PublicClassSquareExtendsShapes {PublicDoubleGetarea () {Return (width * height);}PublicDoubleGetperimeter () {Return (2 * width +2 * height);}PublicSquare (int x,int y,int width,int height) {Super (x, y, width, height);}}PublicClassTriangleExtendsShapes {PublicDouble C;PublicDoubleGetarea () {Return (0.5 * Width * height);}PublicDoubleGetperimeter () {Return (width + height + c);}PublicTriangle (int x,int y,int base,int height) {Super (x, Y, base, height); c = math.sqrt (Width * width + height * height);}}Import Java.applet.Applet;Import Java.awt.Graphics;PublicClassTestExtendsApplet {Square Box =New Square (5,15,25,25); Triangle tri =New Triangle (5,50,8,4); Circle Oval =New Circle (5,80M25,25);Publicvoid paint (Graphics g) {G.drawrect (box.x, box.y, Box.width, box.height); g.DrawString ("box area:" + Box.getarea (), g.drawstring ( "box Perimeter:" + box.getperimeter (), "()," + ), g.drawstring ( "Tri Area:" + Tri.getarea (),  g.drawstring ("tri Perimeter:" + tri.getperimeter (), 50, G.drawoval (oval.x, Oval.y, Oval.width, oval.height); g.DrawString ("Oval area:" + Oval.getarea (), 50 , + );}}                

It can be seen that the class square, Class circle, and Class triangle are derived from the abstract class shape, both of which implement the Getarea and Getperimeter abstract methods.

Use of the Java abstract class

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.