"JAVA" abstract class

Source: Internet
Author: User
Tags getcolor

First, what is abstract class

Classes that are decorated with abstract are abstract classes. Abstract methods that can be useful in abstraction classes, or abstract methods, are not available.

Second, why to design abstract class

In some cases, a parent class just knows what the subclass should contain, but does not know exactly how these subclasses should implement these methods, so it defines some abstract methods, which have no method bodies and no concrete implementations, and must be overridden by subclasses. For example, there is a animal class, it has a method eat (), but different animals eat different ways, so animal can not exactly know how to eat their subclasses. Some people will have doubts, if the parents do not know how to achieve, do not control this method, let the subclass to expand their own. However, there is a problem, if the reference type of the variable is the parent class animal, actually pointing to the subclass of the dog instance, the class cannot call the Eat () method.

Iii. Characteristics of abstract classes

    • Abstract classes must be decorated with abstract.
    • Abstract classes can define field, methods (including common methods and abstract abstract methods), constructors, initialization blocks, and inner classes as normal classes. The constructor of an abstract class is used only for the quilt class invocation, and the abstract class itself is not created as an instance.
    • Classes that contain abstract methods can only be defined as abstract classes.
 PackageAbstractClass; Public Abstract classShape {{System.out.println ("Executes the initialization block of shape. "); }             PublicShape () {System.out.println ("Shape's parameterless constructor"); }     PublicShape (String color) {System.out.println ("Shape's parametric constructor");  This. color = "Red"; }    PrivateString color;  Public StaticString desc = "Shape";  PublicString GetColor () {returncolor; }     Public voidsetcolor (String color) { This. color =color; }     Public Abstract DoubleCalperimeter ();  Public AbstractString GetType (); }

The above defines an abstract class shape, and then defines a subclass of it circle,circle is a normal class that must implement all the abstract methods in shape.

  

 PackageAbstractClass; Public classCircleextendsshape{Private Doubleradius;  PublicCircle (String color,Doubleradius) {        Super(color);  This. Radius =radius; }             Public DoubleGetradius () {return  This. Radius; }     Public voidSetradius (Doubleradius) {         This. Radius =radius; } @Override Public DoubleCalperimeter () {return2 * Math.PI *radius; } @Override PublicString GetType () {returnGetColor () + "Round"; }     Public Static voidMain (string[] args) {Shape s=NewCircle ("Yellow", 3);        System.out.println (S.gettype ());            System.out.println (S.calperimeter ()); }}

An object s is declared in the main method above, the reference type is shape and the circle object is actually pointed, because the getType () and Calperimeter () methods are defined in shape. Therefore, the program can directly invoke the GetType () and Calperimeter () methods in the s variable at run time without forcing the type conversion.

Using abstract classes and abstract methods, we can better play the advantage of polymorphism, is the program more flexible.

Abstract cannot be modified by coexistence with final, static, private modifiers

The method of the abstract modification is to be overridden by the quilt class, and if a method is final, the method cannot be rewritten, so the abstract and final cannot coexist.

If a method is static decorated, it can be called directly by the class name, but the abstract method does not have a method body, which throws an error, so abstract and static cannot coexist.

If the method of the parent class is private, then this method cannot be accessed by the quilt class, much less rewrite, so the abstract and private cannot coexist.

V. The role of abstract classes

Abstract class is a class that is abstracted from multiple classes with the same characteristics, and this abstract class is inherited as a parent class, which is actually a template of subclasses, avoiding the arbitrariness of sub-class design.

Abstract class is an embodiment of template design, and subclasses extend on the basis of abstract class.

  

"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.