Home > database >  What components of the superclass do the subclasses inherit?
What components of the superclass do the subclasses inherit?

Time:10-04

Do all of the subclasses of a superclass inherit the superclass' instance variables, constructors, mutators, and accessors?

Assuming that all members of the superclass is public.

CodePudding user response:

No need to wonder. We have a rather comprehensive language specification at our disposal.

In particular

A class C inherits from its direct superclass type D all concrete methods m (both static and instance) for which all of the following are true:

  • m is a member of D.
  • m is public, protected, or declared with package access in the same package as C.
  • No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m as a member of D.

Notice the highlighted first point. Because when talking about constructors the spec says

Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

Which, back to your question, means that public instance variables and methods (accessors and mutators are not a Java language construct, they're just methods) are inherited. Constructors are not.

  • Related