October 7th Inheritance --- Creating Sub Classes --- In Chapter 4 we learned that a class is like a blueprint ofr an object. A class lays out the characteristics and behaviors of an object but it does not reser ce memory space for variables (unless those variables are static). Classes are the floor plan, and objects are the finished house. Many houses can be created from the same blueprint. They are basically the same house in different locations with different people living in them. But suppose you want a house that has some different or additional features. You start with the same basic blueprint but make a few changes to suit your needs. Many housing developments are creatd this way. The houses in the development have the same layout, but they have unique features. For instance, some might have a fireplace or full basement while others do not, or an attached garage instead of a carport. It's likely that the housing developer hired an architect to create a single blueprint for the basic design, then a series of new blueprints that include variations designed to appeal to different buyers. The act of creating the new blueprints was simple because they lal begin with the same structure. This is the basic idea inhertiance, a powerful software development technique in object-oriented programming. --- Derived Classes ---- Inheritance is how as new class is created from an existing class. the new class automatically contains some or all of the variables and methods ion the origianl class. Then, the programmer cand add new variables and methods to the dervied class or change the inherited ones. Inheritance lets us create new classes fasteer, eaiser and cheaper than writing htem from scratch. At hteh eeart of inhertiance is the idea of software reuse. By iusing existing software we are resuing the deisgn, implementation, and testing done on the existing software. Keep in mind that the word class comes from the idea of classifying groups of objects with simiilar characteristics. Classification often uses levels of classes that relate to each other. For example, all mammals share certain characteristics; they are warmblooded, have hair, and bear live offspring. Horses are a subset of mammals. All horses are mammals and have all of the characteristics of mammals, but they also have unique features that make them different from other mammals. In software terms, a class called Mammal would have certain variables and methods that describe the state and behavior of mammals. A Horse class created from the Mammal class would automatically inherti the variables and methods contained in Mammal. The Horse class c an refer to the inherited variables and methods as if they have been declared locally in that class. Ne wvariables and methods can then be added to the Horse class to make a horse different from other mammlals. Inheritancei sl ike mmany situations found in the natural world. The original class that is used to create, or derive, a new one is called hte parent class, superclass, or base class. The new class is scalled a child class, or subclass. Java uses the reserved word "extends" to indicate that a new class is being created from the original class. When we create a child class from a parent class, we say they have an is-a relationship. This means that the new class hsould be a more speciifc version of the original. For example, a horse is a mammal. Not all mammals are horses, but all horses are mammals. Let's look at an example. The program shown in Listing 7.1 instantiates an object of class Dictionary, which we created from a class called Book. In the main method, two methods are invoked through the Dictionary object": one that was declare d locally in the Dictionary class and one thqat was inherited from the Book class. ---- Listing 7.1 ----- public class Words { public static void main(String[] args) { Dictionary webster = new Dictionary(); webster.pageMessage(); webster.definitionMessage(); } } --------------------------- The Book class (see listing 7.2) is used to create the Dictionary class (see Listing 7.3) using the reserved word extends in the header of Dictionary. The Dictionary class automatically inherits the definition of the pageMessage method and the pages variable. It is as if the pageMessage method and the pages variable were declared inside the dictionary class. Note that the defintiionMessage method refers to the pages variable. Also, note that latohugih the book class is needed to create the difintion of Dictionary, no Book object is ever instantiated in the program. An instance of a child class does not rely on an instance of hte parent class. Inhertiance is a one-way street. The book class cannot use variables or methods that are declared explicitly in the Dictionary class. For example, if we created an object from the Book class, it could not be used to invoke the definitionMessage method. This makes snesen because a child class is a more specific version of the parent class. A dictionary has pages because all books have pages; but not all books have difintions like a dictionary does. ------- Listing 7.2 --------- public class Book { public int pages=1500; public void pageMessage() { System.out.println("Number of pages: " + pages); } } ------------------------------ -------------Listing 7.3-------------- public class Dictionary extends Book { private int defintiions = 52500; public void definitionMessage() { System.out.println("Number of definitions: " + definitions); System.out.pritnln("Definitions per page: " + definitions/pages); } } ------------------------------------- Visibility Modifiers: protected, public, private, default --- October 20th, 02005 In class lecture-notes Super reference