Unformatted text preview:

small lecturenumber - hepage : Code Reusesmall lecturenumber - hepage : Code Reusesmall lecturenumber - hepage : Code Reusesmall lecturenumber - hepage : Inheritancesmall lecturenumber - hepage : Inheritancesmall lecturenumber - hepage : Inheritancesmall lecturenumber - hepage : Inheritancesmall lecturenumber - hepage : Terminologysmall lecturenumber - hepage : Terminologysmall lecturenumber - hepage : Inheritance in actionsmall lecturenumber - hepage : Polymorphismsmall lecturenumber - hepage : Examplesmall lecturenumber - hepage : Examplesmall lecturenumber - hepage : Exercisesmall lecturenumber - hepage : ExerciseIntroduction to Programming IIInheritanceChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??13-2: Code Reuse•Imagine that we’ve built a Person class.•Students have first names and last names.•They also have a printName method that looke like this:public void printName() {System.out.println(firstName + ‘‘ ‘‘ + lastName);}Department of Computer Science — University of San Francisco – p. 2/??13-3: Code Reuse•Now we want to make two new classes: Students andProfessors•It turns out that Professors and Students have the sameprintName method.•So we cut and paste from Person into Student and Professor.•What are some problems with this approach?Department of Computer Science — University of San Francisco – p. 3/??13-4: Code Reuse•Now we want to make two new classes: Students andProfessors•It turns out that Professors and Students have the sameprintName method.•So we cut and paste from Person into Student and Professor.•What are some problems with this approach?◦Extra work - easy for us to make a mistake.◦Making changes is harder; we have to remember all of theplaces we cut and pasted from.◦What if we don’t have access to the Person class?Department of Computer Science — University of San Francisco – p. 4/??13-5: Inheritance•Inheritance lets us solve this problem more elegantly.•One of the core features of OO programming.•Lets us model the world in terms of is − a relationships.◦We can also talk about has − a relationships.•With inheritance, we define one class in terms of another.Department of Computer Science — University of San Francisco – p. 5/??13-6: Inheritancepublic class Person{public String firstName;public String lastName;public String id;public void eat() {System.out.println("Delicious!"); }public void sleep() {System.out.println("zzzzz"); }}Department of Computer Science — University of San Francisco – p. 6/??13-7: Inheritancepublic class Professor extends Personpublic String officeNum;public void teach() System.out.println("blah blah");public void grade() System.out.println("very good!");public void forget() System.out.println("huh??");Department of Computer Science — University of San Francisco – p. 7/??13-8: Inheritance•Professor inherits all of the member variables and methods ofPerson for free.•If Person changes, Professor changes automatically.•A Professor is − a Person.•This is an example of a general-to-specific relationship.Department of Computer Science — University of San Francisco – p. 8/??13-9: Terminology•Professor extends Person•Professor is a subclass of Person.•Professor is a child of Person•Professor derives from or is derived from Person.Department of Computer Science — University of San Francisco – p. 9/??13-10: Terminology•Person is a superclass of Professor•Person is a parent class of Professor.•In Java, all objects implicitly have Object as a parent class.◦What functionality does this provide?Department of Computer Science — University of San Francisco – p. 10/??13-11: Inheritance in actionpublic static void main() {Professor prof = new Professor();prof.eat(); // like all persons, prof can eat (eat is inherited)prof.grade();prof.lastName= "Smith" ; // like all persons, prof has a lastNameprof.officeNum=123;System.out.println(prof.toString()); // like all Objects, prof has toStringPerson person = new Person();person.eat();person.grade(); // error!}Department of Computer Science — University of San Francisco – p. 11/??13-12: Polymorphism•One of the big advantages of OO programming is that it lets ustreat all objects with a common parent identically.•Each object has the same interface, but its own implementation.•This idea is known as polymorphism.•Idea: Every object understands the same messages, but eachobject responds in a way that is appropriate for that object.Department of Computer Science — University of San Francisco – p. 12/??13-13: Examplepublic class Student extends Person {public void sleep() {System.out.println(‘‘Is it time for class yet?’’);}}•Student inherits from Person.•The designer chose to override the behavior of the sleepmethod.Department of Computer Science — University of San Francisco – p. 13/??13-14: ExampleArrayList persons = new ArrayList();Student s = new Student();Professor p = new Professor();persons.add(p);persons.add(s);// print out ids of all personsIterator it = persons.iterator();while (it.hasNext()){Person p = (Person) it.next();p.printName();p.sleep();}Department of Computer Science — University of San Francisco – p. 14/??13-15: Exercise•Make three classes: Shape, Circle, and Rectangle.◦Circle and Rectangle should inherit from Shape.•All shapes should have an x and a y that indicate where the topleft corner of their bounding box goes.•Each shape should have additional data members specific totheir type. (For example, rectangle should have height andwidth.)•Each shape should have an area() method. (For Shape, justreturn 0.0)Department of Computer Science — University of San Francisco – p. 15/??13-16: Exercise•Write a class DrawingGrid - this will be responsible formanaging shapes.•It should contain an ArrayList as a data member.•It should have two methods:◦totalArea - return the sum of the areas of all shapes in thelist.◦main - does the following:•Creates a DrawingGrid•Creates two circles and two rectanges and sets their datamembers appropriately.•Adds the shapes to the DrawingGrid•Prints out the total area used.Department of Computer Science — University of San Francisco – p.


View Full Document

USF CS 112 - Inheritance

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?