Skip to main content
gamedev.net
🔒 Locked

[java] question on objects

Started by omegasyphon 1 reply 300+ views
Original Post
omegasyphon
omegasyphon
ok ill try to make this as easy to understand as possible if i have a class defined as so
  

public class triangle implements Comparable
{
    public triangle();
.
.
.
}

and then another class like so

public class Square
{
   public Comparable[] mySquare;
    
   public Square(int sides)
   {
      mySquare = new Comparable[sides];
      for (int x=0;x<sides;x++)
        mySquare[x]= new triangle();
   }
.
.
.
}

alright 
now how do i go about 
calling an instance of the square class in a main file
and have that object use what ever methods are in the triangle class

for example

    Square s1 = new Square(4); 

   now if i was to do something like this
   where area is a method in the triangle class it doesnt work
   but i dont know if can make it so that u could
    s1.area();










  
Jim_Ross
Jim_Ross
Your stuff makes very little sense to me. Let me try and show some code of what I think you are trying to do, then you can reply back to that.


interface Comparable {
public boolean equals(Comparable c);
public boolean congruent (Comparable c);
}

public class Triangle implements Comparable {
public booleans equals (Comparable c) {
if ((c.sides == 3) && (c.area == this.area)) {
return true;
}
}
...
public void whoami() { System.out.println("I''m a triangle");}
}

public class Square {

public Comparable[] shapes;

public Square() {
this.sides = 4;
this.shapes = new Comparable[5];
this.shapes[0] = new Triangle();
...
}
}

sq1 = new Square();
sq1.shapes[0].whoami();
[/CODE]
krez
krez
you can make a base class "shape", and then derive both "triangle" and "square" from that... then both will have the methods you put in the "shape" class (such as "area", et cetera). it''s been a while since i used java, so no code...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.