lecture12inheritance_4up

lecture12inheritance_4up - Deriving Subclasses CMPT 126...

Info icon This 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 different 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 defining 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 modified (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
Image of page 1

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

View Full Document Right 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 Book class is needed to create Dictionary , no Book object ever need be instantiated.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern