-1

I am making a graphing library in Java (I know there are plenty out there already).

I have an abstract class "Graph" which has variables for width and height and methods to draw the scale and an abstract method to draw the graph. I have multiple classes which extend the Graph class such as LineGraph, BarGraph etc which have a method to draw the graph.

As the width and height of a graph need to be set when a new instance of a graph is created I would like to do it in the constructor but when I tried Eclipse still wanted me to make a separate constructor in each class. Is it possible to have an abstract class with a constructor and how would you do it?

Addict
  • 693
  • 2
  • 7
  • 16
user2248702
  • 2,327
  • 4
  • 32
  • 61
  • 1
    possible duplicate of [Can an abstract class have a constructor?](http://stackoverflow.com/questions/260666/can-an-abstract-class-have-a-constructor) – Taylor Hx Mar 06 '14 at 04:33

5 Answers5

2

You can have constructor in abstract class which takes in width and height. But still you need to have constructor in each of the derived classes which take width and height, but delegates the processing to super class.

You might have to do something like given below,

public abstract class Graph {
   public Graph(int width, int height) {
       // do actual things with width and height
   }
}

public class LineGraph extends Graph{
   public LineGraph(int width, int height) {
      super(width, height);
      // additional inits.
   }
}

public class BarGraph extends Graph{
   public BarGraph(int width, int height) {
      super(width, height);
      // additional inits.
   }
}
  • yes, if the OP has to perform initializations specific to each Graph derivative class, the same can be performed in its constructor after the `super()` is invoked. – asgs Mar 06 '14 at 04:34
1

Its not eclipse. It's the Java Compiler which is forcing you do it. If you have defined the constructor in Abstract Class. One must call that constructor from the derived class.

chauhraj
  • 449
  • 4
  • 19
1

The concept of having a constructor in an abstract helps a programmer to assign a property to all inherited subclass.

usually people use abstract constructor to initialize some property that can use in their children class.

now if in your scenario you want to do so you can use this strategy .

You can refer to this post to understand the concept better -> link

Community
  • 1
  • 1
Mehdi
  • 3,408
  • 3
  • 30
  • 59
0

If you use inheritance, then you can use the base class constructor to assign values of height and width. But in this case you need to either have default constructor in base class along with parametersed constructior or you have to call the parameterised constructor of base class from derived class..

user3256147
  • 338
  • 1
  • 8
-1

For every subclass you need to create a constructor with the same signature (or not) of the super, so if you don't want any behavior in the constructor of the subclasses just add super();

Example:

abstract class Graph {
  int width;
  int height;

  public Graph(int width, int height) {
    this.width = width;
    this.height = height;
  }
}

class LinearGraph extends Graph {
  public LinearGraph(int width, int height) {
    super(width, height);
  }
}
  • That's not right to say: 'you need to create a constructor with the same signature' because you can define a child class which has a different constructor signature with its abstract super class if you call the father with super keyword in child constructor and pass static class variable as a parameter . – Mehdi Mar 06 '14 at 04:52