Math107Ch7 - Introduction To Scientific Programming Chapter...

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

View Full Document Right Arrow Icon
Introduction To Scientific Programming Chapter 7 – Inheritance or (More on Classes)
Background image of page 1

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

View Full DocumentRight Arrow Icon
S.Horton/107/Ch. 7 Slide 2 Lecture Overview On Inheritance I. Inheritance Basics I. Class Hierarchy II. Derived Classes III. Overriding I. Programming With Inheritance I. Methods II. Variables III. Constructors IV. Assignments I. Abstract Classes (skipping interfaces) I. Dynamic Binding & Polymorphism
Background image of page 2
S.Horton/107/Ch. 7 Slide 3 I. Inheritance Basics Inheritance allows you to define a “generic” class and then later define more specialized classes that add new detail relative to the generic class. This “generic” class is called the base or parent class. Specialized classes are derived from the base class and are called derived or child classes The specialized classes inherit all the properties of the base class. After the general class is developed you only have to write the difference or specialization code for each derived class.
Background image of page 3

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

View Full DocumentRight Arrow Icon
S.Horton/107/Ch. 7 Slide 4 Class Hierarchy With a base class and any derived class(es), a structure or hierarchy is established. Any class lower in the hierarchy is a descendent class. A ny class higher in the hierarchy is an ancestor class. This hierarchy can have as many levels as required. That is, a class can be derived from derived classes (child classes can be parent classes!). Note: this class hierarchy exists for all classes including all built-in Java classes/packages .
Background image of page 4
S.Horton/107/Ch. 7 Slide 5 What Does Inheritance Do For Me? Inheritance helps accomplish several of the main goals of object oriented programming (OOP): Reduce the complexity of programs by only requiring “specialized” code for many tasks; Simplify the tasks of writing, testing, and maintaining complex programs by breaking down the problem into sub-parts; Promotes the reuse of classes developed for one application to another application (instead of writing new programs from scratch).
Background image of page 5

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

View Full DocumentRight Arrow Icon
S.Horton/107/Ch. 7 Slide 6 Example Of A Class Hierarchy The base or generic class can be used to implement specialized classes (e.g. student, employee, faculty, …) Classes can be derived from the classes derived from the base class. Person Student Employee Faculty Staff Undergraduate Graduate MastersDegree NonDegree PhD
Background image of page 6
S.Horton/107/Ch. 7 Slide 7 "Is a" and "Has a" Relationships Inheritance is useful for "is a" relationships. A student "is a" person. Student inherits from Person . Inheritance is usually not useful for "has a" relationships. A student "has a(n)" enrollment date. Just add a Date instance variable to Student instead of having Student inherit from a class called Date .
Background image of page 7

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

View Full DocumentRight Arrow Icon
S.Horton/107/Ch. 7 Slide 8 Example Of Inheritance: a Person Base Class public class Person { private String name; //Default constructor public Person() { name = "No name yet."; } //Constructor to init Person name public Person(String initialName) { name = initialName; } //Mutator to set Person name public void setName(String newName)
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/08/2011 for the course MATH 107 taught by Professor Christinashow during the Spring '08 term at Mesa CC.

Page1 / 37

Math107Ch7 - Introduction To Scientific Programming Chapter...

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

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