JAVA - abstract class definition

Keywords: Java

1. abstract class

  • Abstract class, used to represent an abstract concept
  • It is a kind of class that can only define type, but can not produce object, so defining abstract class must have the appearance of subclass
  • The advantage of abstract classes is that they can clearly define the methods that subclasses need to override
  • Abstract classes need to be declared using abstract
  • Abstract methods can be implemented in abstract classes. The characteristics of this method are: only declaration, no method body (so all subclasses must be overridden, and subclasses cannot be abstract classes)

 

For example, the shape class is an abstract concept. Because we cannot calculate the area of this "shape", its member function area() is empty.

The subclass (rectangle, ellipse, triangle, etc.) that inherits it can override the area() member function. In it, the area is calculated by mathematical formula

Refer to the shape class with the following code:

abstract class Shape       //Abstract graphics class
{
         abstract public double area();        //abstract The decorated method is abstract,No need to implement method body.But it must be overridden by subclass
}

class Ellipse extends Shape    //Ellipses
{

         private double wr;  //Radius width of ellipse
         private double hr;  //Radius height of ellipse

         public Ellipse(double wr,double hr)
         {
                  this.wr = wr;
                  this.hr = hr;
         }


         public double area(){
                  return 3.14159*wr*hr;
         }
}


public class Test{

         public static void main(String args[])
         {
                  //Shape s = new Shape();                //If you define an abstract class,Error will be reported

                  Ellipse e = new Ellipse(2.5,1);
                  System.out.println("Ellipse area = "+e.area());
         }
}

 

It should be noted that:

  • Abstract classes can have constructors to initialize their properties,
  • Abstract classes cannot use final definitions,

 

 

2. Abstract inner class definition

In java, if there is only one subclass of our abstract class, we can encapsulate it as an abstract inner class, thus hiding this unknown class for users. It is very common in class libraries

An example is as follows:

abstract class A       //abstract class
{

         abstract public void printData();//abstract The decorated method is abstract,No need to implement method body.But it must be overridden by subclass
static private class B extends A //Inner classes that need to be hidden in abstract classes { private int Data=12; public void printData() { System.out.println("B Data:"+this.Data); //Print internal class's Data value } } static public A getInstance() //Get examples { return new B(); //Obtain the static abstract inner class to be hidden through the static method (Only static variables can be used in static methods) } } public class Test{ public static void main(String args[]) { A a = A.getInstance(); a.printData(); //Equivalent to: A.getInstance().printData(); } }

Print as follows:

 

 

Disadvantages of abstract classes:

  • Because of Java's single inheritance, each class can only inherit from one abstract class

 

Next, I will learn the interface

Posted by robgood on Sat, 16 Nov 2019 12:52:49 -0800