Results 1 to 4 of 4
  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default 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:
    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();
    Thanks.

  2. #2
    Tjstretch is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: Using non-inherited methods of subclass

    You could use casting.
    Eg lets say we have to classes
    Java Code:
    public abstract class Foo
    {
      public abstract void doFoo();
    }
    and
    Java Code:
    public class DefaultFoo extends Foo
    {
      @Override
      public void doFoo()
      {
        System.out.println("A default foo");
      }
    }
    and
    Java Code:
    public class Bar extends Foo
    {
      @Override
      public void doFoo()
      {
        System.out.println("Did foo");
      }
      public void doBar()
      {
        System.out.println("Did bar!");
      }
    }
    Then you had an arraylist holding foo and you want to use doBar, you would use casting.

    Java Code:
    ArrayList<Foo> foosAndBars = new ArrayList<Foo>();
    foosAndBars.add(new DefaultFoo());
    foosAndBars.add(new DefaultFoo());
    foosAndBars.add(new 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 doBar
    Java Code:
    for(Foo foo : foosAndBars)
    {
      foo.doFoo(); 
      if(foo instanceof Bar)
      {
        Bar bar = (Bar) foo; //Cast foo to bar
        bar.doBar();
      }
    }
    This would output
    Java Code:
    A default foo
    A default foo
    A default foo
    Did bar!

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default 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)

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    3

    Default Re: Using non-inherited methods of subclass

    Hmmm. Redesign......
    A massive entity tree or entity-component system...
    ............
    Entity component I think.

Similar Threads

  1. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  2. Inheritence : How the inherited methods behave
    By spartan in forum New To Java
    Replies: 4
    Last Post: 05-23-2011, 08:46 PM
  3. Replies: 9
    Last Post: 09-20-2009, 10:14 AM
  4. [SOLVED] Disabling Inherited Methods
    By Singing Boyo in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 07:04 AM
  5. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 02:16 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
  •