Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which methods can a subclass inherit in Java?

Sorry I am a newbie to Java. I am trying to get my head around inheritance and subclass/superclass relationships in Java.

If classA is a subclass of classB, will classA's protocol feature all methods that belong to classA or only those declared public and package?

Can classA's protocol feature private methods inherited from its superclass ClassB?

like image 469
Genki Avatar asked Jun 13 '11 21:06

Genki


2 Answers

Firstly, the word "inherited" isn't quite the right term. You mean "visible".

  • public and protected are always visible
  • private is not visible
  • default (a.k.a. "package") visibility - ie no specified visibility - is visible only if the subclass is in the same package (as it be would for any class in the same package)
like image 167
Bohemian Avatar answered Oct 28 '22 10:10

Bohemian


All public and protected methods and variables will be inherited. Any methods with the same signature in the subclass will override the superclass behavior. The subclass will not inherit private methods and variables. Default (a.k.a package visibility level) will be inherited if in the same pacakge and by subclasses.

like image 29
Jeff Storey Avatar answered Oct 28 '22 10:10

Jeff Storey