JAVA_SE basics -- 44. Exercise of abstract classes

Source: Internet
Author: User

JAVA_SE basics -- 44. Exercise of abstract classes

Details of abstract classes:

1. If a function does not have a method body, the function must use abstract modifier to modify the function into an abstract function ..
2. If an abstract function appears in a class, the class must also be modified using abstract.
3. If a non-abstract class inherits the abstract class, all abstract methods of the abstract class must be implemented.
4. abstract classes can have non-abstract methods or abstract methods.
5. abstract classes may not have abstract methods.
6. abstract classes cannot create objects.
Q: Why can't abstract classes create objects?
Abstract classes have abstract methods. If you can create objects in abstract classes, use abstract objects.
It makes no sense to call an abstract method.

7. An abstract class has constructor. Its constructor is provided to initialize the attributes of the parent class when the subclass creates an object.

 

 

Exercise: use an abstract class to calculate the area of a rectangle and a circle.

Test code 1: (the comments have been clearly explained, so I will not explain the code one by one ,)

 

Package day09;/* requirement: Describe a graph, circle, and rectangle. No matter which image is used to calculate the area and perimeter, the calculation method of each image is different. * // Define the abstract class of the graphic abstract class Figure {// define the construction method with parameters Figure (String name) {this. name = name ;}// define the nameString name, a member variable of the String type; // define the abstract method getArea () public abstract void getArea (); // define the abstract method getPerimeter () public abstract void getPerimeter ();} // defines the circle class and inherits the class circle extends Figure of the Figure class {// defines the double member variable r double r; // define the constant PI of the double type and assign a value of 3.14 static final double PI = 3.14; // define the construction method circle (String name, double r) {super (name ); this. r = r ;}// implement the abstract method getArea () // define the method for calculating the area of the circle public void getArea () {System. out. println (name + area + PI * r);} // implement the abstract method getPerimeter () // defines the method for calculating the circle perimeter public void getPerimeter () {System. out. println (the circumference of name + is + 2 * PI * r);} // defines the rectangle class and inherits the image (Figure) class rectangle extends Figure {// defines the int type member variable highint high; // defines the int type member variable lengthint length; // defines the rectangle (String name, int high, int length) {super (name); this. high = high; this. length = length;} // implement the abstract method getArea () // define the method for calculating the area of the rectangle public void getArea () {System. out. println (name + area is + high * length);} // implements the abstract method getPerimeter () // defines the method for calculating the perimeter of the rectangle public void getPerimeter () {System. out. println (the circumference of name + is + 2 * (high + length ));}} // define the test class Demo1public class Demo1 {// program entry public static void main (String [] agrs) {// create a circle class instance object, and assign the radius r = 3 Instance Object: create a space for the object and assign it a value of circle c = new circle (circle, 3); // call the method c for calculating the circular area. getArea (); // call the method for calculating the circumference c. getPerimeter (); // create an Instance Object of the rectangle class, and assign a value of the width (high = 2) length (length = 4) rectangle r = new rectangle (rectangle, 2, 4 ); // call the method for calculating the rectangular area r. getArea (); // call the r. getPerimeter ();}}

Running result:

 

Related Article

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.