Java Inheritance Puzzle

Imagine we have one interface and these classes:

interface SuperType {
    void test();
}


class DefaultSuperType implements SuperType{
    @Override
    void test(){
        System.out.println("0")
    }
}

class ChildOfSuperType extends DefaultSuperType{
    @Override
    void test(){
        System.out.println("1")
    }
}

Enter fullscreen mode Exit fullscreen mode

And then we execute this piece of code

SuperType superType = new ChildOfSuperType();
DefaultSuperType defaultSuperType = (DefaultSuperType) superType;
defaultSuperType.test()

Enter fullscreen mode Exit fullscreen mode

Do you know what will be printed?

原文链接:Java Inheritance Puzzle

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容