CH07 - Chapter 7 Java: an Introduction to Computer Science...

Info iconThis preview shows pages 1–8. 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

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: Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 7 Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism Inheritance 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 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 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 Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch An Example of Inheritance: a 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 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)); } } A Person Base Class Display 7.1 Chapter 6 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 7 Java: an Introduction to Computer Science & Programming - Walter Savitch...
View Full Document

Page1 / 31

CH07 - Chapter 7 Java: an Introduction to Computer Science...

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

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