Overridden private method is causing exception in accessing subclass public method in Java -
the below program giving compilation error in line "obj.method()" inside main method. error "the method method() type superclass not visible". understanding should able access public method of subclass. can explain concept behind it?
class superclass{ private void method(){ system.out.println("inside superclass method"); } } public class myclass extends superclass{ public void method(){ system.out.println("inside subclass method"); } public static void main(string s[]){ superclass obj = new myclass(); obj.method(); } }
from understanding should able access public method of subclass.
yes, when compile-time type of expression you're calling on subclass.
so if change code to:
myclass obj = new myclass();
then should fine. currently, compile-time type of obj
superclass
, doesn't have public method
method.
also note myclass.method
not override superclass.method
. call method()
within superclass
call superclass.method()
if actual type of object myclass
.
Comments
Post a Comment