Sub topic 5
Constructors, Fields, and Shared Code in Abstract Classes
You cannot create an object directly from an abstract class. An abstract class can still have fields, constructors, and finished methods. Its subclasses inherit all of them, so you write shared code once instead of copying it into every subclass.
Fields: shared state
A field is a variable that belongs to an object. If you put a field in an abstract class, every subclass object gets that field. For example, every animal has a name. You can store it once in an abstract Animal class and leave the Dog and Cat classes without a name field of their own.
Constructors: setting up shared state
A constructor is special code that runs when an object is created. It usually gives fields their starting values. The constructor of an abstract class never runs alone. It runs only when a subclass object is being built, and the subclass calls it with super(...).
Mark the constructor protected. That keyword means only subclasses (and code in the same package) can call it. Nobody else needs to, because nobody can write new Animal(...) anyway.
Shared code: concrete methods
A concrete method is a method with a body. Concrete methods in an abstract class can use the class's fields. They can even call its abstract methods, and each subclass supplies its own version of those.
A worked example
abstract class Animal {
private final String name;
protected Animal(String name) {
this.name = name;
System.out.println("Animal constructor runs for " + name);
}
public String getName() {
return name;
}
public abstract String sound();
public String speak() {
return name + " says " + sound();
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
System.out.println("Dog constructor runs");
}
@Override
public String sound() {
return "Woof";
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog("Rex");
System.out.println(a.speak());
}
}The output is:
Animal constructor runs for Rex
Dog constructor runs
Rex says WoofThe Dog class has no name field and no speak method. It inherits both from Animal. Because name is private, Dog cannot touch it directly, but it can read it through getName().
The order of construction
The parent's constructor always finishes before the child's constructor continues. That is why the "Animal" line prints first.
Summary
| Part | What it does in an abstract class |
|---|---|
| Field | Holds state every subclass shares |
| Constructor | Sets up that state when a subclass calls `super(...)` |
| Concrete method | Shared behavior that subclasses inherit |
| Abstract method | A step that each subclass must fill in |
Exercise
You remove the line super(name); from the Dog constructor. What happens, and why?
Show answer
The code fails to compile. Without an explicit call, Java tries to call super() with no arguments, but Animal has only one constructor, and that one takes a String. You must call super(name) so Animal's name field gets set.
Exercise
Add a Cat class that extends Animal and makes new Cat("Tom").speak() return "Tom says Meow". What is the smallest Cat class you can write?
Show answer
class Cat extends Animal { Cat(String name) { super(name); } @Override public String sound() { return "Meow"; } }. You need a constructor that passes the name up and an implementation of sound(). Animal already provides speak() and the name field.