Results 1 to 4 of 4
- 12-27-2011, 02:36 AM #1
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Using non-inherited methods of subclass
Hi,
I have an Entity class.
It is abstract private fields and has getters and setters defined and only two abstract methods, update and render.
My other class which inherits Entity has more methods such as movement methods etc.
I have an ArrayList of type Entity declared in a class which adds the Player() class to it.
How can i use the non-inherited methods of the Player() class through an ArrayList().
What i want to do:
Thanks.Java Code:Class holding array: ArrayList<Entity> entities = new ArrayList<Entity>(); entities.add(new Player(param, param)); Class which calls methods from Player(): Class.Class.entities.get(0).nonInheritedFunction();
- 12-27-2011, 03:34 AM #2
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Re: Using non-inherited methods of subclass
You could use casting.
Eg lets say we have to classes
andJava Code:public abstract class Foo { public abstract void doFoo(); }
andJava Code:public class DefaultFoo extends Foo { @Override public void doFoo() { System.out.println("A default foo"); } }
Then you had an arraylist holding foo and you want to use doBar, you would use casting.Java Code:public class Bar extends Foo { @Override public void doFoo() { System.out.println("Did foo"); } public void doBar() { System.out.println("Did bar!"); } }
Now it's clear than only the foo at index 2 would be able to use doBar(). You would have to first check the type of each foo to see if it is a bar, than cast it if it is, than you could call doBarJava Code:ArrayList<Foo> foosAndBars = new ArrayList<Foo>(); foosAndBars.add(new DefaultFoo()); foosAndBars.add(new DefaultFoo()); foosAndBars.add(new Bar());
This would outputJava Code:for(Foo foo : foosAndBars) { foo.doFoo(); if(foo instanceof Bar) { Bar bar = (Bar) foo; //Cast foo to bar bar.doBar(); } }
Java Code:A default foo A default foo A default foo Did bar!
- 12-27-2011, 03:57 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
Re: Using non-inherited methods of subclass
If I understand your question clearly, you can class cast the Entity to a Player...but this sounds like poor design, as at runtime you may not be guaranteed that the Entity at the position in the List you are getting is an instance of Player (the instanceof operator will tell you, but again rethink the design)
- 12-27-2011, 05:23 AM #4
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Similar Threads
-
Override a superclass's methods with a subclass
By zach&kody in forum New To JavaReplies: 7Last Post: 05-24-2011, 02:50 PM -
Inheritence : How the inherited methods behave
By spartan in forum New To JavaReplies: 4Last Post: 05-23-2011, 08:46 PM -
how I transfer/use vars or/and methods from a Thread subclass -to-> the class of main
By lse123 in forum Threads and SynchronizationReplies: 9Last Post: 09-20-2009, 10:14 AM -
[SOLVED] Disabling Inherited Methods
By Singing Boyo in forum New To JavaReplies: 7Last Post: 05-17-2009, 07:04 AM -
hiding inherited methods
By java_fun2007 in forum New To JavaReplies: 1Last Post: 01-05-2008, 02:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks