[JAVA] Abstract class, java Abstract

Source: Internet
Author: User
Tags getcolor

[JAVA] Abstract class, java Abstract

1. What is an abstract class?

The class modified with abstract is an abstract class. Abstract classes can be abstract methods or abstract methods.

Ii. Why design abstract classes?

In some cases, a parent class only knows how its subclass should contain, but cannot accurately know how these subclasses should implement these methods, so it defines some abstract methods, abstract methods do not have a method body and are not implemented in detail. They must be overwritten by subclasses. For example, an Animal class has a method eat (), but different animals eat differently. Therefore, Animal cannot know exactly how its subclass eats. Some people may have doubts. If the parent class does not know how to implement it, don't worry about this method, and let the subclass expand itself. However, there will be a problem. If the variable reference type is the parent class Animal and actually points to the instance of the subclass Dog, then this class cannot call the eat () method.

Iii. abstract class features

  • Abstract classes must be modified with abstract.
  • Abstract classes can define fields, methods (including common methods and abstract methods), constructors, initialization blocks, and internal classes like normal classes. The constructor of the abstract class is only used for calling the quilt class. The abstract class itself cannot create instances.
  • Classes that contain abstract methods can only be defined as abstract classes.
Package abstractClass; public abstract class Shape {System. out. println ("executes the shape initialization block. ");} Public Shape () {System. out. println ("No parameter constructor of shape");} public Shape (String color) {System. out. println ("shape constructors with Parameters"); this. color = "red";} private String color; public static String desc = "shape"; public String getColor () {return color;} public void setColor (String color) {this. color = color;} public abstract double calPerimeter (); public abstract String getType ();}

The above defines an abstract class Shape, and then defines its sub-class Circle. Circle is a common class and must implement all abstract methods in Shape.

  

Package abstractClass; public class Circle extends Shape {private double radius; public Circle (String color, double radius) {super (color); this. radius = radius;} public double getRadius () {return this. radius;} public void setRadius (double radius) {this. radius = radius;} @ Override public double calPerimeter () {return 2 * Math. PI * radius;} @ Override public String getType () {return getColor () + "circle";} public static void main (String [] args) {Shape s = new Circle ("yellow", 3); System. out. println (s. getType (); System. out. println (s. calPerimeter ());}}

In the above main method, an object s is declared, and the reference type is Shape, which actually points to the Circle object, because the getType () and calPerimeter () methods are defined in Shape, therefore, when running, the program can directly call the getType () and calPerimeter () methods in the s variable, without the need for forced type conversion.

Abstract classes and abstract methods allow us to better utilize the advantages of polymorphism, making programs more flexible.

4. abstract cannot coexist with final, static, and private modifiers for modification.

Abstract modification requires that the quilt class be overwritten. If a method is final, this method cannot be overwritten, so abstract and final cannot coexist.

If a method is modified statically, it can be called directly through the class name, but the abstract method does not have a method body, which leads to errors, so abstract and static cannot coexist.

If the method of the parent class is private, this method cannot be accessed by the quilt class, not to mention rewriting. abstract and private cannot coexist.

V. Role of abstract classes

An abstract class is a class abstracted from multiple classes with the same features. This abstract class is inherited as the parent class. It is actually a template of a subclass, avoiding the randomness of the subclass design.

Abstract classes are an embodiment of template design. Child classes are extended based on abstract classes.

  

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.