0

If you are never going to instantiate an object from that class, when are you going to ever use its constructor? Sorry if I come off as ignorant. I just started a class on Java at my high school.

Eric Larson
  • 111
  • 2

4 Answers4

1

you can initialize something in parent class , so maybe you need constructor in abstract class.

1

Because sub classes may use it. For example:

public abstract class Foo {
    protected String name;
    public Foo(String name) {
        this.name = name;
    }
}

public class Bar extends Foo {
    public Bar(String name) {
        super(name); //<-- necessary, otherwise it won't compile
    }

    public Bar() {
        super("default name"); // <-- necessary, otherwise it won't compile
    }
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
0

You have a constructor so subclasses can initialize the state of their parent properly.

public abstract class Parent {
    private final String name;

    public Parent(String n) { this.name = n; }

    public String getName() { return this.name; }
}

public class Child extends Parent {
    public Child(String name) { super(name); }
}

There would be no other way to initialize that private final String name attribute in the Parent without a constructor.

duffymo
  • 293,097
  • 41
  • 348
  • 541
0

Well your parent class or the abstract class stores common variables throught all children classes or subclasses. This makes it easier to store different objects (with the same parent) into collections such as and ArrayList. It also allows you to easily manipulate and object without worrying about its details that is contained in the subclass. You do instantiate the constructor by calling super() within the subclass.