# Inheritence and constructors

Page 1 of 1

## 4 Replies - 913 Views - Last Post: 14 February 2009 - 09:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86694&amp;s=42b3e26d22ca651d5fe18f69c2e8a0c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Nazkav

Reputation: 0
• Posts: 12
• Joined: 14-February 09

# Inheritence and constructors

Posted 14 February 2009 - 07:03 AM

Please could someone help me with this i am new to java and im a bit stuck. here is the code

```public class Point
{
public int x;
public int y;

public Point(int x, int y)
{
this.x=x;
this.y=y;

System.out.println("Constructing a Point.");
}
}

public class GeometricShape
{
public Point centre;

public GeometricShape(int x, int y)
{
System.out.println("Constructing a GeometricShape.");
Point centre = new Point(x,y);
}
public void displayCentre()
{
System.out.println("The x and y coordinates are: " + centre);
}
}

public class Rectangle extends GeometricShape
{
public int width;
public int height;

public Rectangle(int x,int y,int w, int h)
{
System.out.println("Constructing a Rectangle.");
width=w;
height=h;

}
}
public class Square extends Rectangle
{
public String colour;

public Square(int x,int y,int w,int h,String col)
{
colour=col;
System.out.println("Constructing a Square.");
}
public void showColour()
{
System.out.println("The Colour of the Square is: " + colour);
}
}

public class InheritProgram
{
public static void main(String [] args)
{
Square s = new Square(10,20,15,15,"Blue");

s.displayCentre();
s.showColour();
}
}
```

The problem i have is that the result of the x and y coordinates returns null. In the assignment i was asked to pass two of the integers from the Rectangle class' constructor to the parent constructor and i didn't really understand that. here is the output if it helps.

Constructing a GeometricShape.
Constructing a Point.
Constructing a Rectangle.
Constructing a Square.
The x and y coordinates are: null
The Colour of the Square is: Blue

any help would be much appreciated. thanks

Is This A Good Question/Topic? 0

## Replies To: Inheritence and constructors

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Inheritence and constructors

Posted 14 February 2009 - 07:08 AM

```public Rectangle(int x,int y,int w, int h)
{
super(x, y);//<<<i think that's what u were looking for
System.out.println("Constructing a Rectangle.");
width=w;
height=h;

}

```

### #3 Nazkav

Reputation: 0
• Posts: 12
• Joined: 14-February 09

## Re: Inheritence and constructors

Posted 14 February 2009 - 07:20 AM

Thanks for the help but unfortunatly i've already tried that one.

Thanks

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Inheritence and constructors

Posted 14 February 2009 - 07:48 AM

try this out
```public class Point
{
public int x;
public int y;

public Point(int x, int y)
{
this.x=x;
this.y=y;

System.out.println("Constructing a Point.");
}
public void displayCentre()
{
System.out.println("The x and y coordinates are: " + x + " " + y);
}
}

```

```public class GeometricShape extends Point
{

public GeometricShape(int x, int y)
{
super(x,y);
}

}

```

```public class Rectangle extends GeometricShape
{
public int width;
public int height;

public Rectangle(int x,int y,int w, int h)
{
super(x, y);
System.out.println("Constructing a Rectangle.");
width=w;
height=h;

}
}

```

```public class Square extends Rectangle
{
public String colour;

public Square(int x,int y,int w,int h,String col)
{
super(x, y, w, h);
colour=col;
System.out.println("Constructing a Square.");
}
public void showColour()
{
System.out.println("The Colour of the Square is: " + colour);
}
}

```

```public class InheritProgram
{
public static void main(String [] args)
{
Square s = new Square(10,20,15,15,"Blue");

s.displayCentre();
s.showColour();
}
}

```

hope this helps

### #5 Nazkav

Reputation: 0
• Posts: 12
• Joined: 14-February 09

## Re: Inheritence and constructors

Posted 14 February 2009 - 09:29 AM

Thanks a lot that works great. only thing is its a little bit outside the parameters of what was layed down in the my assignment, but i,ll send it in to my tutor and see what he says. Thanks again that was very helpful

nazkav