Study guide: Object-oriented language basics, Part 4
Brush up on Java terms, learn tips and cautions, and review homework assignments
By Jeff Friesen, JavaWorld.com, 07/06/01
Glossary of terms
- implementation inheritance
- The act of extending classes.
- inheritance
- The ability to derive something specific from something generic.
- interface inheritance
- The act of implementing interfaces.
- multiple inheritance
- When an entity directly inherits state and behavior from two or more categories.
- single inheritance
- When an entity directly inherits state and behaviors from one (and only one) category.
- override
- Replace. A subclass method can override (replace) a superclass method.
- subclass
- A class that inherits from another class. Also called a derived class or child class.
- superclass
- A class from which another class inherits. Also called a base class or parent class.
Tips and cautions
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error
messages.
Tips
- To prevent a class from being subclassed, declare that class with the
final keyword. For example: final class BusinessRules { /* appropriate members */ }.
- When deciding whether to use composition or inheritance, remember that inheritance promotes layered objects whereas composition
promotes objects within objects. Assuming composition where inheritance is necessary can lead to redundancy. For example,
thinking of a circle containing a point (composition) instead of being a kind of point (inheritance) results in redundant
circle methods for retrieving the coordinates of the circle's center point.
Cautions
- An error results when a subclass tries to access any private superclass fields and/or methods.
- A subclass cannot extend a final class.
- Because Java does not support multiple implementation inheritance, an error results when a class extends two or more superclasses.
- During a subclass object's initialization, the subclass constructor must pass all superclass layer argument values to the
superclass layer, via a
super (/* list of arguments */); call. Otherwise, the superclass layer will not properly initialize.
- A subclass constructor always calls a superclass constructor. If your subclass has a constructor that does not explicitly
call a superclass constructor, the compiler inserts code to call the default no-argument superclass constructor. If the superclass
declares a constructor that takes at least one argument and does not also declare a no-argument constructor, the compiler
reports an error. There is no no-argument constructor in the superclass for the subclass constructor to call.
- An error results when you call a superclass constructor from a nonconstructor subclass method.
- Never place constructor code ahead of a superclass constructor call (i.e.,
super (/* list of arguments */);); the compiler reports an error.
- When a subclass overrides a superclass method, an error results when the subclass restricts that method's access level. For
example, if the superclass has a
public void draw () method, the compiler reports an error if the subclass changes that method's access level to private, as in private void draw ().
- The compiler reports an error if you attempt to override a final superclass method.
- When assigning a subclass object reference to a reference variable of superclass type, an error results when you attempt to
access subclass members (without an appropriate cast) that don't also appear in the superclass. Without casting the superclass
variable to subclass type before attempting the subclass member access, the compiler has no way to know that the reference
variable has a reference to a subclass object -- instead of a superclass object. For example:
Point p = new Circle (10, 20, 5); System.out.println (p.getRadius ()); causes the compiler to report an error. The getRadius() method would (logically) appear in the Circle class -- not the Point class -- and the compiler checks the p.getRadius() method call against the Point type's members to see if getRadius() is a member of Point (or a Point superclass).
- Casting a superclass reference to a subclass reference is illegal. For example:
Point p = new Point (10, 20); double rad = ((Circle) p).getRadius ();. Although the compiler allows that cast (because the compiler does not know if the reference in p is Point or a subclass, such as Circle), the JVM will note the actual reference type at runtime -- Point -- and generate a ClassCastException object because Point lacks a getRadius() method.
Answers to last month's homework
Last month, I presented you with a question and an exercise. Here are those questions and my answers in red: