DOC PREVIEW
USF CS 112 - Intro to Programming II

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - hepage :} Inheritance Review{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} More examples: Java I/O{small lecturenumber - hepage :} Abstract classes{small lecturenumber - hepage :} Abstract classes{small lecturenumber - hepage :} Abstract classes{small lecturenumber - hepage :} Example{small lecturenumber - hepage :} Dynamic binding{small lecturenumber - hepage :} Dynamic binding{small lecturenumber - hepage :} Constructors and Inheritance{small lecturenumber - hepage :} Constructors and Inheritance{small lecturenumber - hepage :} Constructors and Inheritance{small lecturenumber - hepage :} More on super(){small lecturenumber - hepage :} More on super(){small lecturenumber - hepage :} Exercise{small lecturenumber - hepage :} Next timeIntro to Programming IIMore InheritanceChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p. 1/??14-2: Inheritance Review•Inheritance allows us to reuse existing code.•Allows us to define a hierarchy of classes.•Base class has the most general behavior•Derived classes have more specific behavior.Department of Computer Science — University of San Francisco – p. 2/??14-3: Examplepublic class Person{public String lastName;public String id;public void eat() { };public void sleep() { };}Department of Computer Science — University of San Francisco14-4: Examplepublic class Professor extends Person{public String officeNum;public void teach() { };public void grade() { };public void forget() { };}Department of Computer Science — University of San Francisco – p. 4/??14-5: Example•What if we wanted to make a Student class that was a subclassof Person?◦methods attendClass, doHomework()•What if we also wanted to make a GradStudent that was asubclass of Student?◦New instance variable: public Professor advisor.•Note that Student is an is-a relationship, and advisor is a has-arelationship.Department of Computer Science — University of San Francisco – p. 5/??14-6: More examples: Java I/O•The Java I/O package provides some nice examples ofinheritance.•InputStream is a base class◦Provides read(), skip(), close() methods.◦Very basic functionality.•FileInputStream and FilterInputStream subclass InputStream◦They override the behavior of InputStream for particulardata sources.Department of Computer Science — University of San Francisco14-7: Abstract classes•On Monday, you did a lab in which you created a Shape class◦This had an area() method•You then subclassed it with Circle and Rectangle classes.•One problem with this: you may not want users to ever createShapes◦You just want anything that inherits from Shape to have anarea() method.Department of Computer Science — University of San Francisco – p. 7/??14-8: Abstract classes•Solution: define Shape as an abstract classpublic abstract class Shape{public int locX;public int locY;public abstract double area(); // note: semi-colon, no method body}Department of Computer Science — University of San Francisco – p. 8/??14-9: Abstract classes•An abstract class is one that has one or more abstract methods.◦Can also have concrete methods.•Classes that subclass from an abstract class must override allabstract methods.•An abstract class therefore provides a common interface for aset of subclassesDepartment of Computer Science — University of San Francisco14-10: Example•How would we rewrite Shape as an abstract class?Department of Computer Science — University of San Francisco – p. 10/??14-11: Dynamic binding•What if we have this situation:public class A {public void m1() { }public void m2() { }}public class B extends A {public void m2() { }}B bex = new B();A aex = new B();bex.m1();aex.m1();bex.m2()•Which methods are called?Department of Computer Science — University of San Francisco – p. 11/??14-12: Dynamic binding•Java resolves this via dynamic binding◦The actual type of an object is determined at runtime.◦That object’s class is searched for the correspondingmethod.◦If the method doesn’t exist in that class, the parent class ischecked.•What is the advantage of dynamic binding?•What is the disadvantage?Department of Computer Science — University of San Francisco14-13: Constructors and Inheritance•Suppose that the Person constructor looks like this:public Person(String lname) {lastName = lname;}•How can we still set the last name in derived classconstructors?Department of Computer Science — University of San Francisco – p. 13/??14-14: Constructors and Inheritance•We could do something like this:public Professor(String lname, String office) {lastName = lname;officeNum = office;}•Except:◦We just had to cut and paste code. (we hate that!)◦If the base class changes, all derived classes need tochange.Department of Computer Science — University of San Francisco – p. 14/??14-15: Constructors and Inheritance•Instead, let’s just indicate that the parent class’ constructorshould be called.•We do this with super()public Professor(String lname, String office) {super(lname);officeNum = office;}•Now we don’t need to worry about what the base class’constructor does anymore.Department of Computer Science — University of San Francisco14-16: More on super()•We can also use super to explicitly call a superclass’ method.•This lets us extend a method, rather than overriding it.•For example, let’s say that Person has the following method:/*in Person.java*/public void greet() {System.out.println(‘‘Nice to meet you’’);}Department of Computer Science — University of San Francisco – p. 16/??14-17: More on super()•We’d like for professors to do this greeting, plus a little extra.(They’re long-winded.) So we can do this:/*in Professor.java*/public void greet() {System.out.print(‘‘I do say, old chap,’’);super.greet();}Department of Computer Science — University of San Francisco – p. 17/??14-18: Exercise•Write a base class called Animal. Give it two instance variables(name and furColor). Give it a constructor that sets both ofthese.•Give Animal a printSelf method that prints out the following:◦My name is (name). My fur is (furColor).•Now create a class called Cat that inherits from Animal. Catshould have one instance variable: age.•Write a constructor for Cat that


View Full Document

USF CS 112 - Intro to Programming II

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 Intro to Programming II
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 Intro to Programming II 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 Intro to Programming II 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?