Calling method that exists in child classes but not in parent class

久未见 提交于 2019-11-28 01:58:20

The polymorphism is applied on object reference, not a type. When you call

FooInterface obj = ...// Object of one of the child classes
obj.foo(); 

the child class method foo() is called.

You can't do it with parent object reference until an unless method is declared in parent class/interface itself.

You have to downcast it to child class because parent class/interface doesn't have any knowledge about the child class other than the contract defined between them.

Here contract means abstract methods.


you can try in this way where there is no need to put a check it.

FooInterface sc =new Child1();
sc.foo();

...

interface FooInterface{
    void foo();
}

public class Parent {

}

public class Child1 extends Parent implements FooInterface{

    public void foo() {

    }
}

public class Child2 extends Parent implements FooInterface{

    public void foo() {

    }
}

The approach that I am finally taking is to define an interface say FooInterface with foo() method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo() method like this:

Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface){
    ((FooInterface)obj).foo();
}

If you want to typecast only then there is no need of adding interface. You can typecast it to your desired class and call the method. Example

public class HelloWorld {
    public static void main(String args[]) throws FileNotFoundException {
        SuperClass sc =new Child1();
        if(sc instanceof Child1)//Do same for Child2
        ((Child1)sc).foo();
    }
}

class SuperClass {

}

class Child1 extends SuperClass{
    public void foo(){
        System.out.println("From child1");
    }
}

class Child2 extends SuperClass{
    public void foo(){
        System.out.println("From child2");
    }
}

Output : From child1

You could implement an AbstractChild inheriting from Parent and then extend this class instead of Parent:

public class Parent {
    ....
}

public abstract class AbstractChild extends Parent{

    public abstract void foo();

}



public class Child1 extends AbstractChild {
    ....
    public void foo() {
        ....
    }
}

public class Child2 extends AbstractChild {
    ....
    public void foo() {
        ....
    }
}

So you need to only check if your instance is instanceof AbstractChild.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!