lecture12inheritance_4up - Deriving Subclasses CMPT 126:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT 126: Lecture 9 Inheritance Tamara Smyth, [email protected] School of Computing Science, Simon Fraser University November 15, 2007 1 Deriving Subclasses A class establishes the characteristics and behaviors of an object but reserves no memory space for variables (unless they are declared static ). Classes are the plan, and objects are the embodiment of that plan. Suppose you want to create a plan that is similar to one that already exists, but which has slightly diFerent needs. You will likely begin with the existing plan, and then add variations, according to your needs. The process of deriving a new class from an existing one is called inheritance , and it allows us to reuse existing software. CMPT 126: Inheritance, Lecture 9 2 Inheritance Inheritance is a de±ning characteristic of object-oriented programming. Via inheritance, the new class automatically contains the variables and methods in the original class. To tailor the derived class, new variables and methods can be added, or those that are inherited can be modi±ed (by overriding the method). Its a faster, easier, and cheaper way of writing new classes (rather than writing from scratch) and supports software reuse . CMPT 126: Inheritance, Lecture 9 3 Class Relationship via Inheritance The original class that is used to derive a new one is called the parent class , super class , or base class . The derived class is called a child class , or subclass . The process of inheritance establishes an is-a relationship between the parent and child classes Example: A horse “is a” mammal, but a mammal is not necessarily a horse. Therefore, inheritance is a unidirectional relationship. Java uses the reserved word extends to indicate that a new class is being derived from an existing class. public class Horse extends Mammal The Horse is a subclass of Mammal . CMPT 126: Inheritance, Lecture 9 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dictionary.java and Book.java public class Book { protected int pages = 1500; //Mutator public void setPages(int numPages) { pages = numPages; } //Accessor public int getPages() { return pages; } } public class Dictionary extends Book { private int definitions = 52500; public double computeRatio() { return definitions/pages; } //Definitions mutator public void setDefinitions(int numDefinitions) { definitions = numDefinitions; } //Definitions accessor public int getDefinitions() { return definitions; } } CMPT 126: Inheritance, Lecture 9 5 Dictionary and Book example The Dictionary class is derived from the Book class, using the extends reserved word in the header: public class Dictionary extends Book Notice in the Dictionary class, the computeRatio method explicitly references the pages variable, declared in the Book class. Also, although the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 7

lecture12inheritance_4up - Deriving Subclasses CMPT 126:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online