1

Do subclasses have all the variables and methods of the parent class? Including the Private properties and methods of the parent class?

Or does the subclass have only accessible variables and methods of the parent class? Like public, default, protect?

TOM
  • 11
  • 3

2 Answers2

2

From the Java Language Specification, 8.2:

The members of a class type are all of the following:

  • Members inherited from its direct superclass (§8.1.4), except in class Object, which has no direct superclass

  • Members inherited from any direct superinterfaces (§8.1.5)

  • Members declared in the body of the class (§8.1.6)

The important part about inheritance, regarding private, public and protected members:

Members of a class that are declared private are not inherited by subclasses of that class.**

  • Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

  • Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

Community
  • 1
  • 1
thatguy
  • 21,059
  • 6
  • 30
  • 40
  • thank you ! What is the basis? – TOM Jan 01 '17 at 19:18
  • No, child classes don't inherit private methods and fields. I don't have the JLS to hand, but it says quite explicitly "private fields are not inherited" etc. – Andy Turner Jan 01 '17 at 19:29
  • Here it is, JLS sec 8.2: "Members of a class that are declared private are not inherited by subclasses of that class." – Andy Turner Jan 01 '17 at 19:31
  • @Andy Turner, thank you, looked it up and updated the answer – thatguy Jan 01 '17 at 19:45
  • @AndyTurner I want to find the problem, there is a similar problem. Because my English is not good, I would like to have a good look, and then discuss. Thanks very much .[click here](http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields) – TOM Jan 01 '17 at 19:52
  • @thatguy You too. – TOM Jan 01 '17 at 19:52
  • Your link perfectly describes the mistake I made, too. The JLS is definite. – thatguy Jan 01 '17 at 20:06
0

Subclass can not access parent private properties (fields) and methods. It can access public, protected and default properties and methods only.

Vikas Sachdeva
  • 5,633
  • 2
  • 17
  • 26
  • thank you ! Do subclasses inherit all fields and methods? Or part? – TOM Jan 01 '17 at 19:21
  • All fields and methods which are not private are accessible in child class. Please note that here we are talking about accessibility, not method overriding. – Vikas Sachdeva Jan 01 '17 at 19:27
  • I know the accessibility, I just want to know whether the private fields and methods are inherited – TOM Jan 01 '17 at 19:37
  • I want to find the problem, there is a similar problem. Because my English is not good, I would like to have a good look, and then discuss. Thanks very much [link](http://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields) – TOM Jan 01 '17 at 19:43