A
A
Alexander Chekh2019-03-26 18:32:03
Java
Alexander Chekh, 2019-03-26 18:32:03

Why can't I use the methods of an object of class B (a descendant of A) creating an instance like this "A object = new B()"?

class A{
    void printAString(){
        System.out.println("a string");
    }
}

class B extends A{
    void printBString(){
        System.out.println("b string");
    }
}

class Test1{
    public static void main(String[] args){
        A object = new B();
        System.out.println(object.getClass());
        object.printAString();
        object.printBString();
    }
}

An object of class B, but creating it like this: "A object = new B();" i can't use "printBString()" method.
Why is this happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question