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

    Default constructor inheritance

    Hello,

    I have a bit of a problem with constructor inheritance.

    Java Code:
    public class ArrayPolygon
    {
        private Vertex2D[] vertices;
        
        public ArrayPolygon(Vertex2D[] vertices) {
            if(vertices == null) throw new NullPointerException("NPE");
            this.vertices = new Vertex2D[vertices.length];
            for(int i = 0; i < vertices.length; i++) {
                if(vertices[i] == null) throw new NullPointerException("NPE");
                else this.vertices[i] = vertices[i];
            }
        }
        
        public int getNumVertices() {
            return vertices.length;
        }
        
        public Vertex2D getVertex(int index) throws IllegalArgumentException {
            if(index < 0) throw new IllegalArgumentException("Nespravny index.");
            else return vertices[index%getNumVertices()];
        }
    }
    Now I would like the constructor of this class to take three arguments and pass it to the parent class as an array.
    Here is what Ive tried:

    Java Code:
    public class Triangle extends ArrayPolygon implements Solid
    {
        public Triangle(Vertex2D a, Vertex2D b, Vertex2D c) {
            super(new Vertex2D[] (a,b,c));
        }
    
       // other methods...
    }
    But this code does not work, I have no idea how to write it properly.
    Thank you for any tips or ideas.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: constructor inheritance

    An array literal is surrounded by braces { } not parentheses ( ).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: constructor inheritance

    Well, I tried this:
    Java Code:
    super(new Vertex2D[3] = {a,b,c});
    but then illegal start of expression error occurs. When I try
    Java Code:
    super(new Vertex2D[] = {a,b,c});
    compiler says that array dimension is missing...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: constructor inheritance

    You're declaring a Vertex2D array, not assigning a value to one. Remove the equals sign.

    Also, in respect of your subject line: constructors aren't inherited.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: constructor inheritance

    Ok, thanks for that.
    How come they are not inherited?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: constructor inheritance

    Get rid of the '='.

    ETA: And how long did I have this window open for?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Copy constructor/Inheritance help
    By Devigili22 in forum New To Java
    Replies: 2
    Last Post: 09-06-2012, 04:23 PM
  2. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  3. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  4. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  5. Inheritance and Constructor Problem
    By iratus in forum New To Java
    Replies: 9
    Last Post: 05-10-2011, 07:35 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
  •