Abstract class, the final class introduction (helpful for beginners)

Source: Internet
Author: User
When defining a class, in addition to declaring the class access permissions, you sometimes need to declare the class inheritance features. Declare whether a class is an abstract class or a final class. Use the abstract modifier to declare an abstract class and use the final modifier to declare the final class.

1. Abstract class:

1> declare abstract classes and abstract methods:

Eg. publicpolicactclass planegraphcs1

{

Publicpolicactdouble area (); // a semicolon is required

}

Abstract methods are member methods that only have method declarations but no method bodies. Abstract classes can not contain abstract methods, but classes that contain abstract methods must be declared as abstract classes. Constructor. A class member method cannot be declared as an abstract method.

2> abstract classes cannot be instantiated.

The abstract class cannot be instantiated. Even if the abstract class does not contain the abstract class method, you cannot create an instance of the abstract class. The following statement produces a syntax error:

Planegraphics1 G1 = new planegraphics1 (); // syntax error. The abstract class cannot be instantiated.

 3> abstract classes and abstract methods:

Abstract classes are used to describe abstract concepts. abstract methods are used to declare parameters and return values of methods. The specific implementation of abstract methods is completed by the abstract subclass. The subclass must overwrite the abstract method of the parent class.

Abstract class declaration specifies the method declaration shared by multiple subclasses. each subclass can provide specific implementation of the abstract method based on its actual situation. Obviously, different subclasses can have different implementation methods. Therefore, an abstract method shows polymorphism in multiple subclasses. Abstract classes provide a mechanism for separating method declarations from methods. This allows different sub-classes to exhibit common behavior capabilities.

4> the abstract method must be overwritten by the quilt class:

Abstract methods are not implemented in abstract classes, so abstract methods must be overwritten by the quilt class. If a subclass cannot overwrite the abstract method of the parent class, the class itself must be declared as an abstract class.

5> necessity of abstract methods:

If a method needs to be overwritten by a quilt class, it must be declared as an abstract method. For example, the planegraphics1 class can also declare that the area () method is not an abstract method. The planegraphics1 class does not need to be declared as an abstract class. The statement is as follows:

Public class planegraphics1 // plane graphics class, non-abstract class

{

Public double area () // non-abstract method, must have a method body

{

Return 0;

}

}

Eg.

/*
* This example demonstrates the functions of abstract classes and abstract methods. Declare the abstract class planegraphics1 and the two subclasses. The subclass covers the area () abstract method,
* Calculate the area of different images.
*/
Public abstract class planegraphics1 {// graphic class, abstract class
Private string shape; // shape
Public planegraphics1 (string shape)
{
This. Shape = shape;
}
Public planegraphics1 ()
{
This ("unknown ");
}
Public abstract double area (); // calculated area, abstract method, semicolon ";" Required
Public void print () // display area, non-abstract Method
{
System. Out. println (this. Shape + "area is" + this. Area ());
}
}
// ================================================ ============================================

// Design the rectangle1 rectangle class to inherit the plane graphics class
Public class rectangle1 extends planegraphics1 {
Protected double length; // Length
Protected double width; // width
Public rectangle1 (double length, double width) // Constructor
{
Super ("rectangle ");
This. Length = length;
This. width = width;
}
Public rectangle1 (double width) // a square is a special case of a rectangle.
{
Super ("square ");
This. Length = width;
This. width = width;
}
Public rectangle1 ()
{
This (0, 0 );
}
Public double area () // calculates the rectangular area to implement the abstract method of the parent class.
{
Return this. Width * This. length;
}
}
// ================================================ ======================================

Public class elipse1 extends planegraphics1 {// elliptical class
Protected double radius_a; // a axis radius
Protected double radius_ B; // B axis radius
Public elipse1 (double radius_a, double radius_ B) // Constructor
{
Super ("elliptical ");
This. radius_a = radius_a;
This. radius_ B = radius_ B;
}
Public elipse1 (double radius_a) // The circle is a special case of an ellipse.
{
Super ("circle ");
This. radius_a = radius_a;
This. radius_ B = radius_a;
}
Public elipse1 ()
{
This (0, 0 );
}
Public double area () // calculates the elliptical area to implement the abstract method of the parent class.
{
Return math. Pi * This. radius_a * This. radius_ B;
}
}

// ================================================ ==========================================
Class planegraphics1_ex {

/**
* Use a plane graphics class and subclass
*/
Public static void main (string [] ARGs ){
Planegraphics1 G1 = new rectangle1 (); // obtain the subclass rectangular instance.
G1.print (); // print () indicates the Runtime polymorphism, where the called area () indicates the Runtime polymorphism.
G1 = new rectangle1 (10); // Square
G1.print ();
G1 = new elipse1 (10, 20); // elliptic
G1.print ();
G1 = new elipse1 (10); // circle
G1.print ();
}
}

The running result is as follows:

The rectangular area is 200.0 square meters.
The square area is 100.0 square meters.
The elliptical area is 628.3185307179587 square meters.
The circular area is 314.1592653589793 square meters.

2. Final class:

1> Final Declaration class:

The class declared using the keyword fianl is called the final class. The final class cannot be inherited, that is, the subclass of the final class cannot be declared. Public final class math extends object // mathematical class, final class

If you do not want a class to be inherited, declare the class as the final class.

Abstract classes cannot be declared as final classes.

2> final declaration method:

Using final to declare a member method is called the final method. The final method cannot be overwritten by the quilt class.

Public class circle1 extends graphics1

{

Public final double area () // final method, which cannot be overwritten by the quilt class

{

Return math. Pi * This. radius * This. radius;

}

}

The final class can not contain the final method, but a non-final class can contain the final method.

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.