Introduction to Programming II: Interfaces and Inheritance, Study notes of Computer Science

A portion of a university textbook on programming ii, focusing on interfaces and inheritance. It covers concepts such as inheritance, constructors, and superclass methods, as well as the introduction of interfaces and their implementation. Students will learn how to create classes that extend other classes and implement interfaces, and how to use super() to call parent class constructors and methods.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-ftp
koofers-user-ftp 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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/??
15-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.
Departmentof Computer Science University of San Francisco p. 2/??
15-3: Example
public class Person
{
public String lastName;
public String id;
public void eat() { };
public void sleep() { };
}
Departmentof Computer Science University of San Francisco
15-4: 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/??
15-5: 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/??
15-6: 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
pf3

Partial preview of the text

Download Introduction to Programming II: Interfaces and Inheritance 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

-^

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/

??

Example

public

class

Person

public

String

lastName;

public

String

id;

public

void

eat()

public

void

sleep()

Department of Computer Science — University of San Fra

Example

public

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/

??

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. 5/

??

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 Fra

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 Francisco – p. 7/

??

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. 8/

??

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 Fra

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 takes three arguments: name,furColor, and age. It should set age itself, then call super withthe other two arguments.

-^

Write a method in Cat called printSelf. It should print “I am acat”, then call the superclass’ printSelf method.

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

??

Interfaces

-^

Previously, we talked about abstract classes.

-^

They allow a superclass to specify the methods a subclass willrespond 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 thatinherits from Animal.

-^

We also want to say that Bat is a FlyingThing, and thatFlyingThings respond to the fly() method.

-^

But we already inherited from Animal!

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

??

Interfaces

-^

Interfaces

allow us to specify methods that an object is

guaranteed to respond to, without specifying animplementation.

-^

A class can implement as many interfaces as it wants. public

interface

FlyingThing

public

void

fly();

} public

class

Bat

extends

Animal

implements

FlyingThing

public

void

fly()

System.out.println(‘‘I’m

flying!’’);

Department of Computer Science — University of San Fran