Java inheritance: Reducing visibility in a constructor vs inherited method -
in following code, constructor of child has reduced visibility public private, allowed. inherited methods, such test(), cannot have reduced visibility. why java operate way?
class parent { public parent(){} public void test() { system.out.print("parent test executed!"); } } class child extends parent{ private child(){} private void test(){ system.out.print("child test executed!"); } }
constructors not inherited, child() doesn't override parent().
as methods, if have (if child() public)
parent p = new child(); p.test(); had been allowed, invoking private method. narrowing access while overriding not permitted.
Comments
Post a Comment