Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast an object to class type passed as parameter

I have a parent class and 2 child classes. I am trying to implement a function that takes the type of the child and which child as parameters.

When I use child.newInstance(), I want to store it in a variable of the type that is passed and call a function from the second parameter.

Below are the classes

public class Parent {
    public void test() {
        System.out.println("Test from parent");
    }
}

public class ChildA extends Parent {
    public void testChildA() {
        System.out.println("Test from child a");
    }
}

public class ChildB extends Parent {
    public void testChildB() {
        System.out.println("Test from child b");
    }
}

and here is the method I'm trying to implement

public class Driver {
    Parent func(Class child, String whichChild) throws Exception {
        // whichChild: "ChildA" or "ChildB"

        Object obj = child.newInstance();
        // cast obj to type of child and call the method "test" and "test" + whichChild
    }
}

Can it be done what I am trying to do? If yes, how can I cast this object to the type that is passed?

like image 207
hakuna matata Avatar asked Nov 06 '15 11:11

hakuna matata


People also ask

Can you typecast an object in Java?

Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind or Object, and the process of conversion from one type to another is called Type Casting.

Does casting change the static type?

It only changes when you assign a new object. (It never changes for a given object, no object instance can change its class). The cast bridges the two: It checks the runtime type and then allows you to declare that type.

Can you cast a superclass to a subclass?

You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.


2 Answers

Not sure exactly what you're doing but you can use Class.cast(...).

Eg

public <T> T getInstance(Class<T> type) {
    Object o = type.newInstance();
    T t = type.cast(o);
    return t;
}
like image 148
lance-java Avatar answered Sep 28 '22 02:09

lance-java


If you add a constraint to child, you don't need a cast at all to get a Parent:

Parent func(Class<? extends Parent> child, String whichChild) throws Exception {
    // whichChild: "ChildA" or "ChildB"

    Parent obj = child.newInstance();
    //...
}

However, you still can't call the testChildA etc method, since all you have is an instance of Parent. You'd need to use reflection to get the method:

Method method = obj.getClass().getMethod().getMethod("test" + whichChild);
method.invoke(obj);

It would be better to have a method on the interface of Parent which you can invoke, and is overridden in the subclasses.

public abstract class Parent {
  public void test() {
    System.out.println("Test from parent");
  }

  public abstract void testChild();
}

then simply call:

obj.testChild();

or, as Emanuele Ivaldi points out, just override test in ChildA and ChildB and invoke that directly.

like image 32
Andy Turner Avatar answered Sep 28 '22 01:09

Andy Turner