user1041858
user1041858

Reputation: 957

Call methods of child class in java

How to call print*() of all classes in below scenario means call A's printA(), B's printB() and C's printC() inside the main() method.

class A
{
  public void printA(){System.out.println("A.printA()");}
}

class B extends A
{
    public void printB(){System.out.println("B.printB()");}
}

class C extends A
{
    public void printC(){System.out.println("C.printC()");}
}
class DemoInheritence
{
    public static void main(String[] str){
        printIt(new A());
        printIt(new B());
        printIt(new C());
    }
    public static void printIt(A a) {
        //Here I wants to call A's printA(), B's printB() and C's printC()
        //So how can I do this
    }
}

Upvotes: 3

Views: 4583

Answers (3)

Marcin Pieciukiewicz
Marcin Pieciukiewicz

Reputation: 254

You may use the reflection API, but then you might to make some assumptions. There are at least 2 possibilities:

Solution 1. If print method is called "print[Class name]" you get the method by name:

public static void printIt(Object o) {
    Class objectClass = o.getClass();
    try {
        Method printMethod = objectClass.getMethod("print" + objectClass.getName());
        printMethod.invoke(o);
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException("Method print"+objectClass.getName()+"() doesn't exist", e);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException("Problem calling method print"+objectClass.getName()+"()", e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Method print"+objectClass.getName()+"() is not accessible", e);
    }
}

Solution 2. If Every class always declares ONE method you take first method returned by getDeclaredMethod of passed object's class:

public static void printIt(Object o) {
    Class objectClass = o.getClass();
    try {
        Method printMethod = objectClass.getDeclaredMethods()[0];
        printMethod.invoke(o);
    } catch (InvocationTargetException e) {
        throw new IllegalStateException("Problem calling method print"+objectClass.getName()+"()", e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Method print"+objectClass.getName()+"() is not accessible", e);
    }
}

Upvotes: 0

comanitza
comanitza

Reputation: 1103

For you code you could use something like:

public static void printIt(A a) {
    if(a instanceof B) {
    ((B)a).printB();
    } else if(a instanceof C) {
       ((C)a).printC();
    } else {
      a.printA(); 
    }

}

But this is kinda wrong, you should go with overriding the method in A's child classes, or better yet create an interface that all your classes implement and use that interface in your code.

Upvotes: 4

Oskar Kjellin
Oskar Kjellin

Reputation: 21860

Usually, you do something like this, it's called overriding:

class A
{
  public void print(){System.out.println("A.printA()");}
}

class B extends A
{
    public void print(){System.out.println("B.printB()");}
}

class C extends A
{
    public void print(){System.out.println("C.printC()");}
}
class DemoInheritence
{
    public static void main(String[] str){
        printIt(new A());
        printIt(new B());
        printIt(new C());
    }
    public static void printIt(A a) {
        a.print();
    }
}

Upvotes: 11

Related Questions