Why we have access to A class methods only.Why overriding methods cannot be more restrictive? [closed]

B “is A” A, so you can pass a B to a method like doSomethingToA(A a). The method rightly expects to be able to see m1 and m2.

You only have access to A methods because the type of a is A – it could be an A and not a B, so you can only call the methods the variables class exposes. You coulkd change main to be B a=new B(); and now you can call B methods.

Leave a Comment