Abstract Classes and Methods in Java - Quiz

Total: 5 questions

1. 

What is an abstract class in Java, and can you create an object of it?

An abstract class is a class declared with the abstract modifier. It serves as a common parent for subclasses and describes what they can do without saying how. You cannot instantiate it directly: new Figure(10, 10) is a compile-time error, Figure is abstract; cannot be instantiated, even when the class has a constructor.

Its type can still be used for references to subclass objects, and that is exactly why abstraction exists:

Figure figure = new Rectangle(10, 20); // fine
System.out.println(figure.calculateArea());

The anonymous-class syntax only looks like an exception: new Figure(10, 10) { public double calculateArea() { return 0; } } compiles because it creates an unnamed subclass, not a Figure.

2. 

What is an abstract method, how is it declared, and which modifiers cannot be combined with it?

An abstract method is a method with no implementation: it has a signature but no body. Instead of curly braces you simply put a semicolon:

abstract returnType methodName(parameterList);

public abstract double calculateArea();

The rules: it can only be declared inside an abstract class or an interface; it must be implemented by the first concrete (non-abstract) subclass in the chain — an intermediate subclass may stay abstract and pass the obligation down; and it cannot be static, final or private, because a static method is never overridden, final forbids overriding, and a private method is invisible to subclasses. A method with an empty body { } is not abstract at all — it is an ordinary method that nobody is forced to override.

3. 

Why does an abstract class need a constructor and fields if it never has objects of its own?

The constructor of an abstract class initialises the inherited part of the state and is invoked from a subclass constructor through super(...):

public abstract class Figure {
    double dimension1;
    double dimension2;

    public Figure(double dimension1, double dimension2) {
        this.dimension1 = dimension1;
        this.dimension2 = dimension2;
    }

    public abstract double calculateArea();
}

public class Rectangle extends Figure {
    public Rectangle(double dimension1, double dimension2) {
        super(dimension1, dimension2); // calls the abstract class constructor
    }

    @Override
    public double calculateArea() {
        return dimension1 * dimension2;
    }
}

If the subclass does not call super(...) explicitly, the compiler inserts super() automatically — so if the abstract class has no no-argument constructor, the subclass will not compile. The constructor itself can never be abstract: constructors are neither inherited nor overridden.

4. 

What is the difference between an abstract class and an interface, and which one should you choose?

The key differences:

  • How many: a class can extend only one class, but implement as many interfaces as it likes.
  • State: an abstract class can hold any fields; an interface only public static final constants.
  • Constructor: an abstract class has one, an interface does not.
  • Ready-made code: an abstract class can have any methods with a body; an interface has default, static and (since Java 9) private helper methods.
  • Access modifiers: any in an abstract class, including protected; public by default in an interface.

Since default methods arrived in Java 8 the line has become thinner, but it has not disappeared. Choose an abstract class when subclasses are variations of one entity sharing state and code; choose an interface when you need a contract or a role for otherwise unrelated classes. The JDK uses both at once: List is the interface, AbstractList is the abstract class, ArrayList is the concrete class you instantiate.

5. 

Can an abstract class have no abstract methods at all?

Yes. The rule works in one direction only: a class with at least one abstract method must be abstract, but the reverse is not true — a class can be abstract without a single abstract method:

public abstract class BaseConfig {
    public String name() {
        return "default";
    }
}
// new BaseConfig(); // compile-time error: cannot be instantiated

Such a class still cannot be instantiated — it is a legitimate way of saying "use my subclasses, not me". The JDK does this with java.util.Calendar and with skeletal implementations such as java.util.AbstractList: implement only get(int) and size() and you get a working read-only list with iterator(), contains(), equals() and toString() already provided.

Page 1 of 1