Java Reference
In-Depth Information
Sometimes, the term “immediate superclass” is used to mean the ancestor class, which is one level up in the
inheritance hierarchy whereas the term “superclass” is used to mean an ancestor class at any level. This topic uses
the term “superclass” to mean ancestor of a class, which is one level up in the inheritance hierarchy. For example,
Programmer is the superclass of SystemProgrammer , whereas Employee and Object are ancestors of SystemProgrammer .
Sometimes, the term “immediate subclass” is used to mean a descendant class, which is one level down in the
inheritance hierarchy, whereas the term “subclass” is used to mean a descendant class at any level. This topic uses the
term “subclass” to mean a descendant of a class, which is one level down in the inheritance hierarchy. For example,
Employee is a subclass of Object , whereas Clerk , Programmer , and Manager are subclasses of Employee. Clerk ,
Programmer , ApplicationProgrammer , SystemProgrammer , DatabaseProgrammer , Manager , FullTimeManager , and
PartTimeManager are all descendants of Employee . If a class is a descendant of another class, it is also a descendant of
the ancestor of that class. For example, all descendants of the Employee class are also descendants of the Object class.
All descendants of the Manager class are also descendants of the Employee class and the Object class.
What Is Inherited by a Subclass?
A subclass does not inherit everything from its superclass. However, a subclass may use, directly or indirectly,
everything from its superclass. Let's discuss the distinction between “a subclass inheriting something from its
superclass” and “a subclass using something from its superclass.”
Let's take a real world example. Suppose your parent (a superclass) has money in a bank account. The money
belongs to your parent. You (a subclass) need some money. If you inherit the money, you would just use the money at
will as if the money is yours. If you can just use the money, you cannot get to the parent's money directly. Rather, you
need to ask your parents for money and they will give it to you. In both cases, you used your parent's money. In the
case of inheritance, the money appears to be owned by you. That is, you have direct access to it. In the second case,
your parent's money was available to you for use without you having direct access to it. In the latter case, you had to go
through your parents to use their money.
A subclass inherits non-private members of its superclass. I will discuss this rule in detail shortly. Note that
constructors and initializers (static and instance) are not members of a class, and therefore, they are not inherited.
Members of a class are all members that are declared inside the body of the class and members that it inherits from
the superclass. This definition of members of a class has a trickle-down effect. Suppose there are three classes: A , B ,
and C . The class A inherits from the Object class. The class B inherits from the class A , and the class C inherits from the
class B . Suppose class A declares a private member m1 and a non-private member m2 . The members of class A are m1 ,
m2 , and all inherited members from the Object class. Note that m1 and m2 members of class A are declared members ,
whereas others are inherited members . Members of class B will be any members that are declared in class B and all
non-private members of class A . The member m1 is declared private in class A , so it is not inherited by class B . The
same logic applies to the members of class C . Note that non-private members of the Object class trickle down to class
A , B , and C through the inheritance hierarchy. The non-private members of class A trickle down to class B , which in
turn trickle down to class C , through the inheritance hierarchy.
There are four access modifiers: private , public , protected , and package-level. The absence of the private ,
public , and protected access modifier is considered as the default or package-level access. The access level modifier
of a class member determines two things:
Who can access (or use) that class member directly
Whether a subclass inherits that class member or not
Access modifiers are also used with non-members (e.g. constructors) of a class. In such cases, an access modifier
role is only one: “who can access that non-member?”
If a class member is declared private , it is accessible only inside the class that declares it. A private class
member is not inherited by subclasses of that class.
A public class member is accessible from everywhere in the application. A subclass inherits all public members
of its superclass.
 
Search WWH ::




Custom Search