Skill-Building Exercises


Summary

Inheritance serves three essential purposes: 1) it is an object-oriented design mechanism that enables you to think and reason about your program structure in terms of generalized and specialized class behavior, 2) it provides you with a measure of code reuse within your program by locating common class behavior in base classes, and 3) it serves as a means to incrementally develop programs over time.

Classes that belong to an inheritance hierarchy participate in an “is a” relationship between themselves and their chain of base classes. This “is a” relationship is transitive in the direction of specialized to generalized classes but not vice versa.

The Java class and interface constructs are each used to create new, user-defined data types. The interface construct is used to specify a set of authorized type methods and omits method behavior; the class construct is used to specify a set of authorized type methods and their behavior. A class construct, like an interface, can omit the bodies of one or more of its methods, however, such methods must be declared to be abstract. A class that declares one or more of its methods to be abstract must itself be declared to be an abstract class. Abstract class objects cannot be created with the new operator.

A base class implements default behavior in the form of methods that can be inherited by derived classes. There are three reference -> object combinations: 1) if the base class is a concrete class, meaning it is not abstract, then a base class reference can point to a base class object, 2) a base class reference can point to a derived class object, and 3) a derived class reference can point to a derived class object.

Reference variables have an associated type. Method calls to an object pointed to by a reference will succeed without casting so long as the type of the reference supports the method you are trying to call. You can force, or coerce, the compiler to treat a reference to an object of one type as if it were a reference to an object of another. This is extremely helpful in some circumstances but, as a rule, casting should be used sparingly. Also, casting only works if the object really is of the type you are casting it to.

Derived classes can override base class behavior by providing overriding methods. An overriding method is a method in a derived class that has the same method signature as the base class method it is intending to override. Overriding methods can be called polymorphically via a base class reference that points to a derived class object.

An abstract method is a method that omits its body and has no implementation behavior. A class that declares one or more abstract methods must be declared to be abstract.

The primary purpose of an abstract class is to provide a specification of a set of one or more class interface methods whose implementations are expected to be found in some derived class further down the inheritance hierarchy.

Designers employ abstract classes to provide a measure of application architectural stability.

The purpose of an interface is to specify a set of public interface methods. Interfaces can have four types of members: 1) constants, 2) abstract methods, 3) nested classes, and 4) nested interfaces. Classes can inherit from or extend only one other class, but they can implement as many interfaces as are required. Interfaces can extend as many other interfaces as necessary.

Horizontal and vertical access is controlled via the access specifiers public, protected, and private. A class member without an explicit access modifier declared has package accessibility by default. Essentially, classes belonging to the same package can access each other’s public, protected, and package members horizontally. If a subclass belongs to the same package as its base class it has vertical access to its public, protected, and package members.

Classes belonging to different packages have horizontal access to each other’s public members. A subclass in one package whose base class belongs to another package has horizontal access to the base class’s public methods only but vertical access to both its public and protected members.

Use the final keyword to stop the inheritance mechanism or prevent base class methods from being overridden in derived classes.

Polymorphic behavior is achieved in a program by targeting a set of operations (methods) specified by a base class or interface and manipulating their derived class objects via those methods. This uniform treatment of derived class objects results in cleaner code that’s easier to extend and maintain. Polymorphic behavior is the essence of object-oriented programming.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net