Results 1 to 13 of 13
  1. #1
    green_river48 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default Casting Object to another type

    I have a problem with type casting!
    Java Code:
    public class MyCollection<E>{
    
       protected E[] elements;
       protected int top;
       public static final int SIZE = 10;
       
       public MyCollection() {
            [B]elements = (E[]) new Object[SIZE];[/B]
            top = -1;
        }
    }
    ...
    public class Test {
       
           public void draw(MyCollection<? extends Shape> list){
            for( int i = 0 ; i<=list.top ; i++)
               [B] list.elements[i].[/B]draw(); // compile error - can't cast Object to Shape
            
           }
    
        public static void main(String[] args) {
            MyCollection<Shape> list = new MyCollection<Shape>();
            Test test = new Test();
    
            list.push(new Circle());         // extends Shape , push a new Circle to list
            list.push(new Square());       // extends Shape
            list.push(new Triangle());      // extends Shape
    
            test.draw(list);
        }
    }
    how should i do ?
    Last edited by green_river48; 04-02-2010 at 06:14 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Before that,

    Java Code:
    list.elements[i].draw();
    where you implement this draw() method? In other words, did you implement an interface or what you've done on list?

  3. #3
    green_river48 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Before that,

    Java Code:
    list.elements[i].draw();
    where you implement this draw() method? In other words, did you implement an interface or what you've done on list?
    yeah , this is an abstract method of class Shape and overrided in classes Circle , Square and Triangle

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    green_river48 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I'm confusing. As far as I know there is no such draw() method in the Shape interface.
    if i wrote all the code , it would be very long . So i wrote the most important section :D . Sorry if it made u confused

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by green_river48 View Post
    Java Code:
            [B]elements = (E[]) new Object[SIZE];[/B]
    This is a guess, so sorry if it turns out to be stupid...

    What about...
    Java Code:
            elements = (E[]) new Shape[SIZE];
    ?

    -Gary-

  7. #7
    green_river48 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    This is a guess, so sorry if it turns out to be stupid...

    What about...
    Java Code:
            elements = (E[]) new Shape[SIZE];
    i've just edited :p

  8. #8
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Location
    Delhi
    Posts
    6
    Rep Power
    0

    Default

    i am newbie an have a confusion seeing your code
    Can we have more than one public class in a java program?

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

    Default

    You can have at most one top-level public class in a compilation unit.
    Packages

    db

  10. #10
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Location
    Delhi
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You can have at most one top-level public class in a compilation unit.
    Packages

    db
    Thanks but how green_river is having two public classes in the same program
    are they in different progs. file.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by himanshu19 View Post
    Thanks but how green_river is having two public classes in the same program
    are they in different progs. file.
    Note the ellipses '...' between the two public classes; at least to me that's an indication that both class sources are stored in separate files.

    kind regards,

    Jos

  12. #12
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Location
    Delhi
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Note the ellipses '...' between the two public classes; at least to me that's an indication that both class sources are stored in separate files.

    kind regards,

    Jos
    thank you very much sir for the info:)

    warm regards.

  13. #13
    green_river48 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    i think downcasting only works in reference , it's no use if i try to cast a real Object to Shape, such as : Shape s1 = (Shape) new Ọbject(); and i can't call Shape's method : s1.draw() // casting error :confused:

Similar Threads

  1. Object casting
    By spiderweb in forum Advanced Java
    Replies: 5
    Last Post: 08-20-2009, 05:43 PM
  2. help with type casting.
    By ramsrocker in forum Java Applets
    Replies: 15
    Last Post: 02-26-2009, 11:28 PM
  3. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 07:07 PM
  4. type casting array?
    By willemjav in forum Advanced Java
    Replies: 5
    Last Post: 08-27-2008, 10:25 PM
  5. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM

Posting Permissions

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