Home > Object-Oriented Programming: Inheritance > Tips >
     
Object-Oriented Programming: Inheritance
Tips


Common Programming Errors

9.1 It is a syntax error to override a method with a more restricted access modifier. A public method of the superclass cannot become a protected or private method in the subclass.
9.2 A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not match exactly the number and types of parameters specified in one of the superclass constructor declarations.
9.3 When a superclass method is overridden in a subclass, the subclass version often calls the superclass version to do additional work. Failure to prefix the superclass method name with the keyword super and a dot (.) when referencing the superclass's method causes infinite recursion, because the subclass method would then call itself.
9.4 Declaring a superclass public or protected method with a different parameter list in the subclass hides the superclass version of the method. Attempts to call the superclass version result in compilation errors.

Back to Tips



Good Programming Practices

9.1 Use the protected access modifier when a superclass should provide a service (i.e., a method) only to its subclasses and should not provide the service to other clients.
9.2 Declare superclass instance variables private (as opposed to protected) so that the superclass implementation can change without affecting subclass implementations.

Back to Tips



Performance Tip

9.1 If subclasses are larger than they need to be (i.e., contain too much functionality), memory and processing resources might be wasted. Extend the class with the functionality that is "closest" to what is needed.

Back to Tips



Software Engineering Observations

9.1 Methods of a subclass cannot access private members of their superclass.
9.2 Declaring private data helps programmers test, debug and correctly modify systems. If a subclass could access its superclass's private data, classes that inherit from that subclass could access data as well. This would propagate access to what should be private data, and the benefits of information hiding would be lost.
9.3 The Java compiler sets the superclass of a class to Object when the program does not specify a superclass explicitly.
9.4 When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor (explicitly, via super, or implicitly). The superclass constructor's body executes to manipulate the superclass's instance variables, and the subclass constructor's body executes to manipulate the subclass's instance variables. Java ensures that all instance variables are initialized with their default values. Thus, even if a constructor does not assign a value to an instance variable, the variable is still initialized.
9.5 At the design stage in an object-oriented system, the designer often finds that certain classes are closely related. The designer should "factor out" common attributes and behaviors and place these in a superclass. Then, the designer should use inheritance to develop subclasses, endowing them with capabilities beyond those inherited from the superclass.
9.6 Declaring a subclass does not affect its superclass's source code. Inheritance preserves the integrity of a superclass.
9.7 Just as designers of non-object-oriented systems should avoid a proliferation of methods, designers of object-oriented systems should avoid the proliferation of classes. Proliferation of classes creates management problems and can hinder software reusability, because it becomes difficult for a client to locate the most appropriate class in a huge class library. The alternative is to create fewer classes that provide more substantial functionality, but such classes might provide too much functionality.

Back to Tips



Error-Prevention Tip

9.1 When possible, avoid including protected instance variables in a superclass. Rather, include non-private methods that access private instance variables, ensuring that the object maintains a consistent state.

Back to Tips





Copyright © 1995-2008, Pearson Education, Inc., publishing as Pearson Prentice Hall Legal and Privacy Terms