Chapter8 Notes

Chapter8 Notes - 1 1 Inheritance Inheritance is a...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 Inheritance ¡ Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes ¡ Chapter 8 focuses on ¢ deriving new classes from existing classes ¢ the protected modifier ¢ creating class hierarchies ¢ abstract classes ¢ indirect visibility of inherited members ¢ designing for inheritance 2 Outline • Creating Subclasses • Overriding Methods • Class Hierarchies • Inheritance and Visibility • Designing for Inheritance 2 3 8.1 – Inheritance ¡ Inheritance allows a software developer to derive a new class from an existing one ¡ The existing class is called the parent class, or superclass , or base class ¡ The derived class is called the child class or subclass ¡ As the name implies, the child inherits characteristics of the parent ¡ That is, the child class inherits the methods and data defined by the parent class 4 8.1 – Inheritance ¡ Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class ¡ Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent 3 5 8.1 – Inheritance ¡ A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones ¡ Software reuse is a fundamental benefit of inheritance ¡ By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 6 8.1 – Deriving Subclasses ¡ In Java, we use the reserved word extends to establish an inheritance relationship class Car extends Vehicle { // class contents } 4 7 8.1 – Words.java //******************************************************************** // Words.java Java Foundations // // Demonstrates the use of an inherited method. //******************************************************************** public class Words { //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String args) { Dictionary webster = new Dictionary(); System.out.println ("Number of pages: " + webster.getPages()); System.out.println ("Number of definitions: " + webster.getDefinitions()); System.out.println ("Definitions per page: " + webster.computeRatio()); } } 8 8.1 – Book.java //******************************************************************** // Book.java Java Foundations // // Represents a book. Used as the parent of a derived class to // demonstrate inheritance....
View Full Document

This note was uploaded on 03/31/2008 for the course IST 240 taught by Professor Tracymullen during the Spring '08 term at Penn State.

Page1 / 27

Chapter8 Notes - 1 1 Inheritance Inheritance is a...

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

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