{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Math107Ch7 - Introduction To Scientific Programming Chapter...

Info icon This 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)
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
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
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.
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
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 .
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).
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
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
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 .
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
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) { name = newName; } //Accessor to get Person name public String getName() { return name; }
Image of page 8
Image of page 9
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