Inheritance


  • Inheritance is the process of creating new classes, called derived classes, from existing or base classes. It is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and embellishing these with capabilities the new classes require.
  • The derived class inherits all the capabilities of the base class but can add enhancements.
  • The base class is unchanged by this process.
  • Inheritance is one of the most powerful features of object-oriented programming.
  • Keyword extends implements inheritance.
  • New classes can inherit from class libraries.
  • Multiple inheritance is not supported by Java.  (Interfaces let the programmer obtain the effect of multiple inheritance.)

Using Inheritance

  • When inheritance is used to create a new class from an existing class, the new class inherits the attributes and behaviors of an existing class.  Attributes and behaviors can be added or superclass behaviors can be overridden to customize the class to meet new needs.
  • Creating a subclass does not affect its superclass's source code.
  • A superclass specifies commonality.
  • All classes derived from a superclass inherit the capabilities of that superclass.
  • In the object-oriented design process, the designer looks for commonality among a set of classes and factors it out to form desirable superclasses.
  • Subclasses are then customized beyond the capabilities inherited from the superclass.
  • Inherited members are not shown in subclass declarations, but inherited members are nevertheless present in the subclasses.

Superclasses and Subclasses

  • A superclass supplies attributes and behaviors to other classes.
  • When creating a new class, instead of writing completely new instance variables and instance methods, the programmer can designate that the new class is to inherit the instance variables and instance methods of a previously defined superclass.
  • The new class is referred to as a subclass.
  • A subclass normally adds instance variables and instance methods of its own, so a subclass is generally larger than its superclass.
  • A subclass is more specific than its superclass and represents a smaller group of objects.
  • The real strength of inheritance comes from the ability to define in the subclass additions to, or replacements for, the features inherited from the superclass.
  • A superclass exists in a hierarchical relationship with its subclasses.
Example:

A university community consists of employees, students and alumni. Employees are either faculty members or staff members. Faculty members are either administrators (such as deans and department chairpersons) or teaching faculty. This yields the inheritance hierarchy shown below.

  • The arrows in the hierarchy represent the "is a" relationship.
  • CommunityMember is the direct superclass of Employee, Student and Alumni.
  • CommunityMember is an indirect superclass of all the other classes in the hierarchy diagram.

  • Starting from the bottom of the diagram, you can follow the arrows and apply the "is a" relationship all the way up to the topmost superclass in the hierarchy. 
  • An Administrator is a Faculty member, is an Employee, is a CommunityMember, and is an Object (because all classes in Java have Object as one of their direct or indirect superclasses.)

Protected Members

  • Public members of a superclass are accessible anywhere that the program has a reference to that superclass type or one of its subclass types.
  • Private members of a superclass are accessible only by methods of the superclass.
  • Protected access serves as an intermediate level of protection between public access and private access.
  • Protected members of a superclass may be accessed not only by methods of the superclass but also by methods of subclasses and by methods of other classes in the same package.
  • Subclass methods can normally refer to public and protected members of the superclass simply by using the member names.
  • A subclass inherits all of the non-private members of the superclass, except the constructors.
  • The public interface of a subclass includes all of the public methods of the superclass as well as any overloaded constructors and the public methods of the subclass.
  • Note: When a subclass method overrides a superclass method, the superclass method may be accessed from the subclass by preceding the superclass method name with super followed by the dot operator (.).
  • A protected member can be accessed by member functions in its own class or in any class derived from its own class. It can't be accessed from methods outside these classes.
  • Recall that class members (instance variables or methods) can always be accessed by methods within their own class, whether the members are private or public.  But objects of a class defined outside the class can access class members only if the members are public.
  • With inheritance, member functions can access members of the base class if the members are public, or if they are protected. They can't access private members.
  • In summary, if you are writing a class that you suspect might be used as a base class for other classes, then any data or methods that the derived classes might need to access should be made protected rather than private.
  • Note, however, that protected members are considerably less secure than private members.
  • Anyone with access to your library of classes can access protected members of those classes simply by deriving other classes from them.
  • Note again that the base class remains unchanged and neither it nor its objects know anything about any classes derived from the base class.
  • For more details see this link.

 

Argument against the promiscuous use of protected...

Most object-oriented applications declare their fields private, thereby not allowing other objects to access their fields directly. Then they make their methods protected, public, or package as needed and allow other objects to manipulate their private data by calling the methods only. This way, you can update your class by changing a field definition and the corresponding method implementation, but other objects that access that data do not need to be changed because their interface to the data (the method signature) has not changed. 

While protected access can be applied to any member of a class, it is usually reserved for methods and constructors.  If applied to fields, that would be a weakening of encapsulation.  Wherever possible, mutable fields in superclasses should remain private.  There are, however, occasional valid cases where direct access by subclasses is desirable.  

This is especially evident in situations such as the julianDate class, which requires a setDate method in order validate and convert data before setting the instance variable.  Direct access to the instance variable may not be desired.  

Direct access to the instance variables of the superclass may not be desirable in cases where the superclass implementation changes but the  interface remains constant.

 

 

Constructors in Subclasses

  • Every subclass constructor is required to call its direct superclass's constructor as its first task either implicitly or explicitly.  If there is no explicit call to the superclass constructor, Java automatically attempts to call the superclass's default constructor. 
  • When an object of a subclass is instantiated, the superclass’s constructor is responsible for doing any necessary initialization of the superclass instance variables that were inherited by the subclass object.
  • An explicit call to the superclass constructor (via the super( ); statement) must be provided as the first statement in the subclass constructor--otherwise the subclass constructor will call the superclass default constructor (or no-argument constructor) implicitly. 
  • Superclass constructors are not inherited by subclasses, but must be called via the super( ); statement.
  • When an object of a subclass is created, first the subclass constructor calls the superclass constructor, the superclass constructor executes, then the remainder of the subclass constructor's body executes.

Finalizers in Subclasses

  • If both the superclass and subclass contains a finalize method, the subclass method should invoke the superclass finalize method (as its last action) to insure that all parts of an object are finalized properly when the garbage collector reclaims resources for the object.
  • Method finalize should always be defined as protected so subclasses have access to the method.

Method Overloading and Overriding 

A method's signature refers to the number, types and order of the arguments in the parameter list.

A subclass can redefine a superclass method using the same signature, i.e., methods of a subclass can have the same name as those in the superclass.

Method overriding: When the same method name exists in both the superclass and the subclass, when that method is called in the subclass, the method in the subclass will be executed, i.e., the subclass method overrides the superclass method. 

If the method in the superclass is needed, it can be called by prefacing the method call with the super keyword followed by the dot operator.

Example:  super.finalize( );

========================

If a redefinition of a superclass method in a subclass does not have the same signature as the superclass method, it is not method overriding, but method overloading.  


Inherited Items (and how to hide stuff)

The following list itemizes the members that are inherited by a subclass:


Abstract Superclasses and Concrete Classes

  • Abstract classes are classes which are defined but for which the programmer never intends to instantiate any objects.
  • These normally serve as superclasses in inheritance situations, and are referred to as abstract superclasses.
  • The sole purpose of an abstract class is to provide an appropriate superclass from which other classes may inherit interface and/or implementation.
  • Abstract superclasses are too generic to define real objects; we need to be more specific before we can think of instantiating objects.
  • A class is made abstract by declaring it with the keyword abstract.
  • No objects of abstract superclasses can be instantiated.
  • Classes from which objects can be instantiated are called concrete classes.
  • Concrete classes provide the specifics that make it reasonable to instantiate objects.

Final Methods and Classes

  • A class that is declared final cannot be a superclass--no class can inherit from a final class.
  • Every method in a final class is implicitly final.
  • Final methods cannot be overridden in a subclass.
  • Methods that are declared static and methods that are declared private are implicitly final.