Docsity

Introduction to Programming II - Interference | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
Intro to Programming II
Interfaces
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
16-0: 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.
Departmentof Computer Science —University of San Francisco – p.2/??
16-1: Example
public class Person
{
public String lastName;
public String id;
public void eat() { };
public void sleep() { };
}
Departmentof Computer Science —University of San Francisco – p.3/??
16-2: Example
public class Professor extends Person
{
public String officeNum;
public void teach() { };
public void grade() { };
public void forget() { };
}
Departmentof Computer Science —University of San Francisco – p.4/??
16-3: 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 class
constructors?
Departmentof Computer Science —University of San Francisco – p.5/??
16-4: 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 to
change.
Departmentof Computer Science —University of San Francisco – p.6/??
pf3

Partial preview of the text

Download Introduction to Programming II - Interference | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II

Interfaces

Chris Brooks

Department of Computer Science

University of San Francisco

?? Department of Computer Science — University of San Francisco – p.1/

Inheritance review 16-0:

Inheritance allows us to reuse existing code.

Allows us to define a hierarchy of classes.

has the most general behavior Base class

have more specific behavior. Derived classes

?? Department of Computer Science — University of San Francisco – p.2/

Example 16-1:

Person class public

lastName; String public id; String public

; { } eat() void public

; { } sleep() void public

?? Department of Computer Science — University of San Francisco – p.3/

Example 16-2:

Person extends Professor class public

officeNum; String public

; { } teach() void public

; { } grade() void public

; { } forget() void public

?? Department of Computer Science — University of San Francisco – p.4/

Constructors and Inheritance 16-3:

Suppose that the Person constructor looks like this:

{ lname) Person(String public

lname; = lastName

How can we still set the last name in derived class

constructors?

?? Department of Computer Science — University of San Francisco – p.5/

Constructors and Inheritance 16-4:

We could do something like this:

{ office) String lname, Professor(String public

= lname; lastName office; = officeNum

Except:

We just had to cut and paste code. (we hate that!)

If the base class changes, all derived classes need to

change.

?? Department of Computer Science — University of San Francisco – p.6/

Constructors and Inheritance 16-5: Instead, let’s just indicate that the parent class’ constructor should be called. We do this with super()

{ office) String lname, Professor(String public

super(lname); office; = officeNum

Now we don’t need to worry about what the base class’ constructor does anymore. ?? Department of Computer Science — University of San Francisco – p.7/ More on super() 16-6: We can also use super to explicitly call a superclass’ method. a method, rather than overriding it. extend This lets us For example, let’s say that Person has the following method: / Person.java in /

{ greet() void public

you’’); meet to System.out.println(‘‘Nice

?? Department of Computer Science — University of San Francisco – p.8/ More on super() 16-7: We’d like for professors to do this greeting, plus a little extra. (They’re long-winded.) So we can do this: / Professor.java in /

{ greet() void public

chap,’’); old say, do System.out.print(‘‘I super.greet();

?? Department of Computer Science — University of San Francisco – p.9/ Exercise 16-8: Write a base class called Animal. Give it two instance variables (name and furColor). Give it a constructor that sets both of these. 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. Cat should have one instance variable: age. Write a constructor for Cat that takes three arguments: name, furColor, and age. It should set age itself, then call super with the other two arguments. Write a method in Cat called printSelf. It should print “I am a cat”, then call the superclass’ printSelf method. ?? Department of Computer Science — University of San Francisco – p.10/ Interfaces 16-9: Previously, we talked about abstract classes. They allow a superclass to specify the methods a subclass will respond to without providing an implementation. Sometimes abstract classes can be awkward to deal with. For example, let’s say we want to create a class called Bat that inherits from Animal. We also want to say that Bat is a FlyingThing, and that FlyingThings respond to the fly() method. But we already inherited from Animal! ?? Department of Computer Science — University of San Francisco – p.11/ Interfaces 16-10: allow us to specify methods that an object is Interfaces guaranteed to respond to, without specifying an implementation. A class can implement as many interfaces as it wants.

{ FlyingThing interface public

fly(); void public

{ FlyingThing implements Animal extends Bat class public

{ fly() void public

flying!’’); System.out.println(‘‘I’m

?? Department of Computer Science — University of San Francisco – p.12/