Concepts / Abstract Methods

Abstract Methods

An abstract class is declared with the abstract modifier and is meant to be subclassed rather than instantiated on its own.

  • Programming

A Class That Is Deliberately Unfinished

Most classes you write are complete. You create an object from them and they do their job. Some classes are different. Their author knows that part of the behavior is clear in concept but depends on the specific kind of thing being described, so the author leaves that part unwritten on purpose. Java calls this an abstract class, and the unwritten part is called an abstract method.

This article follows a short Java program built around a Shape class. The central question to keep in mind is this: which parts of Shape are complete, and which parts are left for someone else to finish? Answering that question is the key to understanding how abstract classes and abstract methods work together.

Predicting What a Subclass Inherits

What do you think happens?

Shape has a complete setColor method and an unfinished redraw method. If you write a subclass that extends Shape and writes nothing else, which of these two methods can it use without writing any code?

  • Only redraw

  • Only setColor

  • Both setColor and redraw

  • Neither method

Reveal answer
Answer:

Only setColor

The setColor method is fully written inside the abstract class, so every subclass inherits working color-changing behavior. The redraw method has no body, so the subclass must supply one before it can be a finished, usable class.

Tracing the Shape Class

Run the program below in your head before reading the explanation. The Shape class has two methods. The first, setColor, is complete. It stores a new color in the color instance variable and then calls redraw. The second, redraw, is marked abstract and has no body. Its declaration ends with a semicolon where the braces and code would normally go.

java
Notes on the code
  • The abstract modifier in the class header marks Shape as incomplete by design.
  • setColor has a full body, so every subclass inherits it unchanged.
  • redraw has no body. Its line ends with a semicolon instead of braces.

Follow the flow when setColor is called. Java runs the color assignment first, then calls redraw. Because redraw has no body inside Shape, the actual drawing code must come from whichever concrete subclass the object really is. The call is written once in the parent class, but the behavior it produces is supplied later by the child.

Declaring an Abstract Class

A class becomes abstract when you add the abstract modifier to its declaration. The general form is public abstract class ClassName followed by a body in braces. The modifier tells the compiler that the class is incomplete on purpose. The class is designed to be the parent of other classes rather than a finished product on its own.

Definition

An abstract class is a class declared with the abstract modifier. It is meant to be subclassed rather than instantiated on its own.

What do you think happens?

If the main method contains the line new Shape(); where Shape is the abstract class above, will the program compile?

  • Yes, it compiles

  • No, it does not compile

Reveal answer
Answer:

No, it does not compile

An abstract class is meant to be subclassed rather than created directly. You create objects from a concrete subclass such as one that extends Shape and supplies every missing method.

The abstract class is useful even though it cannot be used directly. It can contain complete methods, such as setColor, that every subclass shares. This avoids writing the same working code in several places. Only the parts that truly vary by kind are left open.

Reading an Abstract Method Declaration

An abstract method is declared with the abstract keyword and without an implementation. The ordinary method body, which would contain braces and code, is replaced by a semicolon. In the Shape example the full line is abstract void redraw(); and the general syntax is abstract return-type method-name(parameters);. Compare the pieces of that declaration below.

The source describes redraw as semantically abstract. The idea of redrawing a shape is clear, but the way a particular shape is drawn depends on what kind of shape it is. Marking the method abstract makes that dependence visible in the code. At the compiler level, the meaning is simple: the method has no body in this class, and a subclass has to supply one.

Supplying the Missing Method in a Subclass

A concrete subclass is a class that extends the abstract class and is not itself abstract. The example below is a generated illustration rather than part of the source. Circle extends Shape and supplies the missing redraw implementation, so it is a finished class that can be created with new.

java
Notes on the code
  • extends Shape makes Circle inherit setColor without rewriting it.
  • Circle is not declared abstract, so it must provide a body for redraw.
  • The body of redraw here satisfies the requirement that the abstract method be implemented.

Once Circle exists, calling setColor on a Circle object runs the inherited setColor body, which then calls redraw. Java finds the redraw body in Circle, so the drawing step completes. The abstract requirement from Shape is satisfied at the point where Circle supplies its own version.

Why Every Concrete Subclass Must Implement the Method

The rule is strict. Every concrete subclass must implement each abstract method it inherits. If a class extends Shape, is not abstract itself, and does not provide a redraw body, then the design expectation set by Shape is broken. The class has inherited an unfinished piece of behavior and never finished it. This is why a missing implementation is the most likely place to look when a hierarchy behaves unexpectedly.

In this hierarchy, Circle and Square both inherit setColor and both inherit the abstract redraw. Circle satisfies the rule. Square does not, because it is concrete but has no body for redraw. Square is the class where the expectation fails, even though Shape itself was written correctly.

Finding the Broken Link in a Deeper Hierarchy

Hierarchies can be longer than two levels. An intermediate class may itself be abstract and may leave redraw unimplemented without any error, because abstract classes are allowed to pass the job further down. The rule only applies at the point where a class is concrete. To find the failing class, walk down the chain and check each class in order.

Polygon is allowed to leave redraw open because it is abstract. Hexagon is the first class that breaks the expectation. It is concrete, it inherits the abstract redraw through Polygon and Shape, and it provides no body for it. The fix belongs in Hexagon, not in Shape or Polygon.

MEDIUM

A class Triangle extends Polygon, is declared without the abstract modifier, and contains no redraw method. Which class in the chain is the first to break the design expectation, and what single change would repair it?

Hints
  • Ask first whether each class is abstract or concrete.
  • Only a concrete class must provide a body for every inherited abstract method.
  • Polygon is abstract, so the failing class is the first concrete one below it.

Abstract Method Versus Concrete Method

AspectConcrete methodAbstract method
BodyHas braces and codeHas no body; ends with a semicolon
What a subclass inheritsWorking behavior, ready to useA required slot with no behavior yet
What a concrete subclass must writeNothing, unless it wants to overrideA body for the method
Where it can appearAny classOnly in an abstract class

Comparing a concrete method such as setColor with an abstract method such as redraw

The table makes the practical difference clear. A concrete method passes working behavior down to every subclass. An abstract method passes down a requirement. Both appear in the same class, which is why Shape can offer finished behavior and unfinished behavior together.

Mistakes Beginners Make with Abstract Methods

  • Common mistake

    Giving the abstract method an empty body with braces

    What it looks like
    public abstract void redraw() { }
    Why it fails

    An abstract method is declared without an implementation. Braces mean a body is present, so the method is no longer abstract in the intended sense.

    Do this instead
    public abstract void redraw();
  • Common mistake

    Leaving out the abstract modifier on a class that has an abstract method

    What it looks like
    public class Shape { public abstract void redraw(); }
    Why it fails

    A class with an abstract method must itself be declared abstract. The modifier is what marks the class as incomplete by design.

    Do this instead
    public abstract class Shape { public abstract void redraw(); }
  • Common mistake

    Assuming an abstract class can be created directly

    Why it fails

    An abstract class is meant to be subclassed rather than instantiated on its own.

    Do this instead

    Create objects from a concrete subclass such as Circle.

  • Common mistake

    Forgetting that a concrete subclass must implement every inherited abstract method

    Why it fails

    If the concrete subclass has no body for the inherited abstract method, the expectation set by the parent is not met.

    Do this instead

    Add a body for each inherited abstract method in the concrete class, or declare that class abstract and push the job to its own subclass.

  • Common mistake

    Looking for the bug in Shape when the real gap is in a lower concrete class

Debugging a Hierarchy That Misbehaves

When an abstract-class design behaves differently than expected, the fastest route is to ask three questions in order. Each question narrows the search to a single line of code.

  1. 1
    Is the class declared abstract? Check the class header for the abstract modifier.
  2. 2
    Does the method being called have an abstract declaration with a semicolon instead of a body? Find the declaration of the method at the top of the chain.
  3. 3
    Does the concrete subclass that is actually running provide an implementation for that method? Check the lowest concrete class in the chain for a body.
EASY

Predict which of the three questions will find the problem in this case: an object of class Hexagon is created, calling setColor on it produces an error, and Shape declares redraw as abstract with a semicolon. Explain your answer in one sentence.

Hints
  • Think about which class the object really is when redraw is called.
  • The first two questions have already been answered in this scenario.

Keeping the Three Core Ideas Together

  1. 1
    The abstract modifier marks a class as meant to be subclassed rather than instantiated on its own.
  2. 2
    An abstract method has no body and ends with a semicolon, as in abstract void redraw();.
  3. 3
    Every concrete subclass must supply an implementation for each inherited abstract method, and the first concrete class that does not is where the design expectation breaks.

Key Takeaways

  • An abstract class is declared with the abstract modifier and is meant to be subclassed rather than instantiated on its own.
  • An abstract class can hold complete methods, such as setColor, that every subclass inherits without rewriting them.
  • An abstract method is declared with the abstract keyword and ends with a semicolon in place of a body.
  • Every concrete subclass must implement each abstract method it inherits, so the first concrete class that does not is the likely failure point.