Results 1 to 11 of 11
  1. #1
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default constructors and inheritance

    Hello,

    I have a class called GeneralRegularPolygon with my own constructor.

    Java 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;
    }
    Now I want to create RegularOctagon class extending the upper one

    Java Code:
    public class RegularOctagon extends GeneralRegularPolygon {
    
        public RegularOctagon(Vertex2D center, int numEdges, double edgeLength) {
            super.getCenter() = center;
            super.getNumEdges() = numEdges;
            super.getEdgeLength() = 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 length

    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.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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

  3. #3
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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:

    Java Code:
    private Vertex2D center;
    private double edgeLength;
    
    public RegularOctagon(Vertex2D center, double edgeLength) {
         this.center = center;
         this.edgeLength = edgeLength;
    }
    then I get the above error.
    Last edited by dawnMist; 10-24-2012 at 09:23 PM.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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!
    	}
    }

  5. #5
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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...

  6. #6
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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!

  7. #7
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default 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";   
        .
        .
        .
    }
    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;
        }
    }
    The error here says that radius has private access in GeneralRegularPolygon.
    How so? Why can´t I use these private fields?
    Last edited by dawnMist; 10-25-2012 at 12:42 AM.

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: constructors and inheritance

    You're going to need setters and getters for private variables.

    Java Code:
       public double getRadius(){
            return radius;
       }
    etc.

    (You may also want to look at your super() call from Circle constructor, you're setting the edgeLength in GeneralRegularPolygon to the input radius.)

  9. #9
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: constructors and inheritance

    Thanks for that, I corrected it.

    Now I have this piece of code:

    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;
        }
    }
    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.
    Is there anyway how to do this?

  10. #10
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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 ?!

  11. #11
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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

  1. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 12:05 PM
  2. using constructors
    By droidus in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 11:30 PM
  3. constructors
    By droidus in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 09:14 PM
  4. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 03:14 PM
  5. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •