Results 1 to 11 of 11
Thread: constructors and inheritance
- 10-24-2012, 04:47 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
constructors and inheritance
Hello,
I have a class called GeneralRegularPolygon with my own constructor.
Now I want to create RegularOctagon class extending the upper oneJava Code:public class GeneralRegularPolygon implements RegularPolygon, Colored { private Vertex2D center; private double radius; private int numEdges; private double edgeLength; private String color = "white"; public int getNumEdges() { return numEdges; } public double getEdgeLength() { return edgeLength; } public Vertex2D getCenter() { return center; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public GeneralRegularPolygon(Vertex2D center, int numEdges, double edgeLength) { this.center = center; this.numEdges = numEdges; this.edgeLength = edgeLength; }
But this brings an error: constructor cannot be applied to given types; required Vertex2D,int, double - found: no arguments; reason: actual and foral argument lists differ in lengthJava Code:public class RegularOctagon extends GeneralRegularPolygon { public RegularOctagon(Vertex2D center, int numEdges, double edgeLength) { super.getCenter() = center; super.getNumEdges() = numEdges; super.getEdgeLength() = edgeLength; } }
Where is the problem here? And how should I get the class RegularOctagon to create a special instance of GeneralRegularPolygon?
Thank you for any tips.
- 10-24-2012, 06:14 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: constructors and inheritance
public RegularOctagon(Vertex2D center, int numEdges, double edgeLength) {
super(center, numEdges, edgeLength);
}
Please read Inheritance (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance) again
- 10-24-2012, 07:15 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: constructors and inheritance
And what if I need to inherit only center and edgeLength? Because clearly the numEdges = 8 for octagon.
When I try to write another constructor with two parameters:
then I get the above error.Java Code:private Vertex2D center; private double edgeLength; public RegularOctagon(Vertex2D center, double edgeLength) { this.center = center; this.edgeLength = edgeLength; }Last edited by dawnMist; 10-24-2012 at 08:23 PM.
- 10-24-2012, 09:05 PM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: constructors and inheritance
By default super(); is called even if you don't tell it to!
Call the super constructor you want as the first line of your constructor.
Java Code:super(center, 8, edgeLength);
Enumerated (a bit)
Java Code:public class Sandbox { public static class Joe{ public int myX; public int myY; public Joe(int x, int y){ // Joe's only constructor myX = x; myY = y; } } public static class Jeff extends Joe{ public Jeff(int x){ super(x, 3); //basically, Jeff = new Joe(x, 3) } } public static void main(String[] args) { Jeff j1 = new Jeff(1); //new Jeff System.out.print(j1.myY); //new Jeff's myY = 3! } }
- 10-24-2012, 09:38 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: constructors and inheritance
Ok, thanks for this, now I get it.
My question is - is there any way of writing the constructor with only two parameters?
Because I am supposed to (the test class used to check my work tests it like this "RegularOctagon s = new RegularOctagon(new Vertex2D(1.0, -1.0), 2.0);" // Vertex2D is my class representing a point via X and Y coordinates) and I cannot see a way how...
- 10-24-2012, 10:30 PM #6
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: constructors and inheritance
Yes.
Look at the above example, Jeff only has one parameter in its only constructor, whereas Joe has two. ....
Jeff extends Joe
super(x, 3) in Jeff calls the Joe(x, y) constructor!
- 10-24-2012, 10:51 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: constructors and inheritance
Of course! Thank you very much, you´ve been very helpful!
The last thing for now - how can I inherit private fields?
Java Code:public class GeneralRegularPolygon implements RegularPolygon, Colored { private Vertex2D center; private double radius; private int numEdges; private double edgeLength; private String color = "white"; . . . }The error here says that radius has private access in GeneralRegularPolygon.Java Code:public class Circle extends GeneralRegularPolygon { public static final int NUM_EDGES = Integer.MAX_VALUE; public Circle(Vertex2D center, double radius) { super(center, NUM_EDGES, radius); } public double getArea() { return Math.PI * radius * radius; } }
How so? Why can´t I use these private fields?Last edited by dawnMist; 10-24-2012 at 11:42 PM.
- 10-25-2012, 02:33 AM #8
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: constructors and inheritance
You're going to need setters and getters for private variables.
etc.Java Code:public double getRadius(){ return radius; }
(You may also want to look at your super() call from Circle constructor, you're setting the edgeLength in GeneralRegularPolygon to the input radius.)
- 10-25-2012, 11:14 AM #9
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: constructors and inheritance
Thanks for that, I corrected it.
Now I have this piece of code:
and I want the Circle class to use the getHeight() and getWidth() methods from class GeneralRegularPolygon, but with the radius value of the class Circle. I cannot write these two methods again in the Circle class.Java Code:public class GeneralRegularPolygon { private Vertex2D center; private double radius; private int numEdges; private double edgeLength; public double getRadius() { return (edgeLength)/(2 * Math.sin(Math.PI/numEdges)); } public double getHeight() { return 2 * getRadius(); } public double getWidth() { return 2 * getRadius(); } } public class Circle extends GeneralRegularPolygon { private double radius; public double getRadius() { return this.radius; } }
Is there anyway how to do this?
- 10-25-2012, 06:05 PM #10
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: constructors and inheritance
?? You don`t need more methods? You don`t have to write methods, you can just call the getHeight and getWidth methods on a circle instance ?!
- 10-25-2012, 11:39 PM #11
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: constructors and inheritance
Your circle class should contain the radius, and @Override the getHeight() and getWidth() from it's parent class.
The Polygon class should not have a radius! For that matter, is a circle a polygon? It has one side! (Two if you say inside and outside, but that's a different discussion).
Similar Threads
-
Constructors
By EdOBannon in forum Advanced JavaReplies: 2Last Post: 12-15-2011, 11:05 AM -
using constructors
By droidus in forum New To JavaReplies: 1Last Post: 09-18-2011, 10:30 PM -
constructors
By droidus in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:14 PM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks