Java Object Oriented Design - Java Abstract Classes and Methods

Java can define a class whose objects cannot be created.

Its purpose is just to represent an idea, which is common to objects of other classes.

Such a class is called an abstract class.


We need to use the abstract keyword in the class declaration to declare an abstract class.

For example, the following code declares a Shape class abstract:

public abstract class Shape  {


The following code adds a draw() method to Shape class.

public abstract class Shape  {
    public  Shape() {

    public abstract  void  draw();

A abstract class does not necessarily mean that it has at least one abstract method.

If a class has an abstract method either declared or inherited, it must be declared abstract.

An abstract method is declared the same way as any other methods, except that its body is indicated by a semicolon.


The following Shape class has both abstract and non-abstract methods.

abstract class Shape {
  private String name;
//from   w  w w .  j a v  a  2  s. com
  public Shape() { = "Unknown  shape";

  public Shape(String name) { = name;

  public String getName() {

  public void setName(String name) { = name;

  // Abstract methods
  public abstract void draw();

  public abstract double getArea();

  public abstract double getPerimeter();

The following code shows how to create a Rectangle class, which inherits from the Shape class.

class Rectangle extends Shape {
  private double width;
  private double height;

  public Rectangle(double width, double height) {
    // Set the shape name as "Rectangle"
    this.width = width;
    this.height = height;

  // Provide an implementation for inherited abstract draw() method
  public void draw() {
    System.out.println("Drawing a  rectangle...");

  // Provide an implementation for inherited abstract getArea() method
  public double getArea() {
    return width * height;

  // Provide an implementation for inherited abstract getPerimeter() method
  public double getPerimeter() {
    return 2.0 * (width + height);