Java Inheritance Puzzle

devit951

Ildarov

Posted on April 2, 2019

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?

💖 💪 🙅 🚩
devit951
Ildarov

Posted on April 2, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Java Inheritance Puzzle
java Java Inheritance Puzzle

April 2, 2019