Salamandar
Salamandar

Reputation: 588

Extended class constructor

I got a very complex class called Line, and a extended one, ColouredLine that just adds a colour to it.

How can I create a ColouredLine from a Line in the constructor ?

Here is my code (that doesn't work…)

private class ColouredLine extends Line {
    private Color color;
    public ColouredLine(Line line, Color color) {
        this = (ColouredLine)line;
        this.color = color;
    }

}

Thanks !

Upvotes: 1

Views: 142

Answers (3)

Jobs
Jobs

Reputation: 3377

Improper usage of the 'this' keyword:

'this' can only be used in one of two ways:

  1. To avoid confusion when a method of constructor parameter shares the same name as their fields. For example:

public void change(int age) {
    String name = "Tom";

    this.age = age;
    this.name = name;
  }

  1. Explicit constructor invocation: From within a constructor, you can also use the this keyword to call another constructor in the same class.

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}

'this' can never be used by itself, or to invoke a constructor from a super class.

Instead, use the Super() keyword to call the modified super constructor. Before doing so, you have to modify the super class constructor to take a Line object in its parameter, like this,

public Line (Line line)

And in the first line of the sub class constructor, simply call the super constructor:

super(line);

Note that without defining a new constructor, the default constructor public Line() is always going to be called in all of its subclasses.

Upvotes: 0

lvr123
lvr123

Reputation: 584

Use delegate model. No need to modify the base Line class.

private class ColouredLine extends Line {
    private Color color;
    private final Line line;

    public ColouredLine(Line line, Color color) {
        this.line = line;
        this.color = color;
    }

    /**
    * Delegates Line methods to the line object
    **/
    @Override
    public int getProperty1() {
        return line.getProperty1();
        }

    /**
    * Specific ColouredLine methods 
    **/
    public Color getColor() {
        return color;
        }
}

Upvotes: 0

Eran
Eran

Reputation: 393821

You would need a copy constructor in the Line class :

public class Line 
{
    ...
    public Line (Line line)
    {
        // copy properties of line
        this.something = line.something ...
    }
    ...
}

Then in ColouredLine's constructor, you call Line's copy constructor :

private class ColouredLine extends Line {
    private Color color;
    public ColouredLine(Line line, Color color) {
        super (line);
        this.color = color;
    }

}

Upvotes: 4

Related Questions