{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CH07 - Chapter 7 Inheritance Inheritance Basics Programming...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 7 Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism Inheritance
Image of page 1

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

View Full Document Right Arrow Icon
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Principles of OOP  OOP - Object-Oriented Programming Principles discussed in previous chapters: » Information Hiding » Encapsulation In this chapter » Inheritance
Image of page 2
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Why OOP? To try to deal with the complexity of programs To apply principles of abstraction to simplify the tasks of writing, testing, maintaining and understanding complex programs To increase code reuse » to reuse classes developed for one application in other applications instead of writing new programs from scratch ("Why reinvent the wheel?") Inheritance is a major technique for realizing these objectives
Image of page 3

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

View Full Document Right Arrow Icon
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Inheritance Overview Inheritance allows you to define a very general class then later define more specialized classes by adding new detail » the general class is called the base or parent class The specialized classes inherit all the properties of the general class » specialized classes are derived from the base class » they are called derived or child classes After the general class is developed you only have to write the "difference" or "specialization" code for each derived class A class hierarchy: classes can be derived from derived classes (child classes can be parent classes) » any class higher in the hierarchy is an ancestor class » any class lower in the hierarchy is a descendent class
Image of page 4
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch An Example of Inheritance: Person  Class The base class: Display 7.1 Constructors: » a default constructor » one that initializes the name attribute (instance variable) Accessor methods: » setName to change the value of the name attribute » getName to read the value of the name attribute » writeOutput to display the value of the name attribute One other class method: » sameName to compare the values of the name attributes for objects of the class Note: the methods are public and the name attribute private
Image of page 5

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

View Full Document Right Arrow Icon
public class Person { private String name; public Person() { name = "No name yet."; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.name)); } } Person Base Class Display 7.1 Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch
Image of page 6
Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Derived Classes: a Class Hierarchy The base class can be used to implement specialized classes
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}