0

I have a base class Component and I have classes such as ComponentA, ComponentB, etc. inheriting from this class. I store the components in a HashMap with a key of the component name and a value of the component. If I get the value of ComponentA and perform functions on it, however, it treats it as a Component class. Is there away to typecast the Component to ComponentA to execute the methods of ComponentA or do I need to look into an alternate method of storing my components?

Cody Short
  • 41
  • 1
  • 1
  • 7

3 Answers3

1

You have an object of type component?

Component c = //some component

The type cast is simple, it's just

ComponentA a = (ComponentA)c

Cruncher
  • 7,641
  • 1
  • 31
  • 65
1

Use the @Override annotation to make sure you are actually overriding the methods of the base class:

public class Component {
    ...

    public void doSomething() {
        ...
    }
}

public class ComponentA extends Component {
    ...

    @Override
    public void doSomething() {
        ...
    }
}

P.S you shouldn't need to do any casts. One benefit of polymorphism is that it allows you to use objects of different classes through a common base class. A cast is for when you need functionality in a derived class that the base class has no concept of. Using a cast for functionality exposed through the base class just defeats that benefit.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • Yes, also look at [when-do-you-use-javas-override-annotation-and-why](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – asgs Nov 24 '12 at 06:16
1

If you are storing ComponentA in your map as Component then your object is still ComponentA. In that case, you can do the type casting but I would recommend to check the instance type as blow:

        Component element = map.get(componentKey);
        if(element instanceOf ComponentA){
           ComponentA elementA = (ComponentA)element;
           //use the elementA
           elementA.doSomething();
        }else if (element instanceOf ComponentB){
           ComponentB elementB = (ComponentB)element;
           //use the elementB
           elementB.doSomething();
        }

In addition, if you override the required methods from Component to ComponentA then you don't need to do the type casting. As I mentioned earlier, your element is still of type ComponentA and hence the overridden method in ComponentA will be called.

e.g.

  public class Component{
     public void printClass(){
         System.out.println("This is class Component");
     }
  }

  public class ComponentA{
     @Override
     public void printClass(){
        System.out.println("This is class ComponentA");
   }
  }

    Map<String, Component> map= new HashMap<String,Component>();
    Component component = new ComponentA();
    map.put("comp", component);
    Component component1 = map.get("comp");
    component1.printClass(); //<-- prints "This is class ComponentA"
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73