|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 9 of 10
Point p = new Point (); double radius = ((Circle) p).getRadius ();
The problem: The Point object has no notion of a getRadius() method because Circle introduces that method -- and it is not part of Point. Although this code will compile, when it runs, the JVM will generate an exception -- a failure -- of type ClassCastException when the getRadius() method call occurs. If the JVM was not designed to take such action, the attempt to call a nonexistent getRadius() method would most likely crash the JVM software.
Inheritance and composition relate to each other in a manner similar to two sides of a coin. Inheritance promotes a layered object, like an onion. In contrast, composition promotes objects within objects, like lumpy porridge, where each lump represents an object. Last month, you learned that composition results from "has a" relationships between entities. In contrast, inheritance results from "is a" relationships. When you encounter text like "a car is a kind of vehicle" or "a savings account is a kind of bank account," you are dealing with inheritance. You can use both inheritance and composition to help design sophisticated objects. Neither is exclusive of the other; developers use them together.
One of the harder tasks a developer faces when designing a class hierarchy is deciding when to use composition and when to use inheritance. Sometimes, "is a" and "has a" relationships aren't clear. For example, suppose you didn't know that a circle is a point with a radius. Instead, in your mind a circle has a point and a radius. Working from that premise, you might write the following code fragment: