Java Abstract Class

Java Abstract class

  • An abstract class is a class where some or all methods do not have any code written for them, and such classes must be notified with the abstract modifier in front.
  • An abstract class cannot be instantiated as an object.
  • The methods with no implementation are called abstract methods and must be notified with the abstractt modifier in front.
  • A sub-class which extends the abstract class must provide the implementations of all those methods. To not do so means that the sub-class also is abstract.
  • A class that is not an abstract class is called a concrete class, and an abstract class can extends a such concrete class.
    Abstract class example:
    // This is an abstract class that cannot be instantiated
    abstract class Shape {
    
      abstract public double GetArea();
    
      abstract public double GetPerim();
    
      public void Draw() {
        System.out.println("Abstract drawing mechanism!");
      }
    }
    // Circle is a concrete class that implements the 
    // abstract methods in the abstract class shape
    
    class Circle extends Shape {
    
      private final int itsRadius;
    
      public Circle(int radius) {
        itsRadius = radius;
      }
    
      @Override
      public double GetArea() {
        return 3.14 * itsRadius * itsRadius;
      }
    
      @Override
      public double GetPerim() {
        return 2 * 3.14 * itsRadius;
      }
    
      @Override
      public void Draw() {
        System.out.println("Circle drawing routine here!");
        super.Draw();
      }
    }
    // Rectangle is a concrete class that implements the 
    // abstract methods in the abstract class shape
    
    class Rectangle extends Shape {
    
      private final int itsWidth;
      private final int itsLength;
    
      public Rectangle(int len, int width) {
        itsLength = len;
        itsWidth = width;
      }
    
      @Override
      public double GetArea() {
        return itsLength * itsWidth;
      }
    
      @Override
      public double GetPerim() {
        return 2 * itsLength + 2 * itsWidth;
      }
    
      public int GetLength() {
        return itsLength;
      }
    
      public int GetWidth() {
        return itsWidth;
      }
    
      @Override
      public void Draw() {
        for (int i = 0; i < itsLength; i++) {
          for (int j = 0; j < itsWidth; j++) {
            System.out.print("x ");
          }
          System.out.print("\n");
        }
        super.Draw();
      }
    }
    
    public class AbstractDemo {
    
      public static void main(String args[]) {
        int choice;
        boolean fQuit = false;
        Shape sp = null;
        while (fQuit == false) {
          System.out.print("(1)Circle (2)Rectangle (0)Quit: ");
          Scanner scan = new Scanner(System.in);
          choice = scan.nextInt();
          switch (choice) {
            case 1:
              sp = new Circle(4);
              break;
            case 2:
              sp = new Rectangle(4, 6);
              break;
            default:
              fQuit = true;
          }
          if (fQuit == false) {
            System.out.println("Area:"+ sp.GetArea()+" and perimeter:"+sp.GetPerim());
            sp.Draw();                
            System.out.println();
          }
        }
      }
    }
    The result of this is:
    (1)Circle (2)Rectangle (0)Quit: 1
    Area:50.24 and perimeter:25.12
    Circle drawing routine here!
    Abstract drawing mechanism!
    
    (1)Circle (2)Rectangle (0)Quit: 2
    Area:24.0 and perimeter:20.0
    x x x x x x 
    x x x x x x 
    x x x x x x 
    x x x x x x 
    Abstract drawing mechanism!
    
    (1)Circle (2)Rectangle (0)Quit: 0
    You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.