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

后端 未结 5 2030
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 02:11
public class Parent {
    ....
}

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

public class Child2 extends Parent {
           


        
相关标签:
5条回答
  • 2020-12-07 02:23

    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.

    0 讨论(0)
  • 2020-12-07 02:39

    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

    0 讨论(0)
  • 2020-12-07 02:40

    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.

    0 讨论(0)
  • 2020-12-07 02:41

    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() {
    
        }
    }
    
    0 讨论(0)
  • 2020-12-07 02:43

    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();
    }
    
    0 讨论(0)
提交回复
热议问题