• Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoking subclass method on object in a linked list  RSS feed

 
Duncan MacFarland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass? Thanks.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is. It's called generic collections.The part inside < and > specifies the type that can go into the list and the type you will get from the list. So there is no need to cast.


Prior to Java 7 you would need to write
Beginning with Java 7 you can skip the type on the right side of =.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duncan MacFarland wrote:I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass? Thanks.



I'm not sure if Pawel got your problem.
Is it something like you have a class A, a subclass of A named B, and a LinkedList, but want to insert an object of B at position, say, 5, and want the JVM to know that this is of type B, not A?
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
Duncan MacFarland wrote:I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass? Thanks.



I'm not sure if Pawel got your problem.
Is it something like you have a class A, a subclass of A named B, and a LinkedList, but want to insert an object of B at position, say, 5, and want the JVM to know that this is of type B, not A?


Oh I get it. I was confused by the fact that OP wrote This produces an error because Object does not have that method.
Object with capital letter tricked me into believing that OP meand Object class.
So I thought OP is using raw collections.

So, Duncan, you can't do what you want. You need to cast it every time.
But why would you need this in a first place? Maybe
you should reconsider your design.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and welcome to Coderanch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!