Docsity

Interfaces and Inheritance in Java: Using super() and Implementing Interfaces, Study notes of Computer Science

Partial preview of the text

Download Interfaces and Inheritance in Java: Using super() and Implementing Interfaces and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II

Interfaces^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of 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.

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

16-2:^ 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/

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 classconstructors?

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

16-5:^

Constructors and Inheritance

Instead, let’s just indicate that the parent class’constructor should 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/

16-6:^

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 followingmethod: /*^ in^ Person.java

public^

void^ greet()

System.out.println(‘‘Nice

to^ meet

you’’);

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

16-8:^

Exercise

Write a base class called Animal. Give it two instancevariables (name and furColor). Give it a constructor thatsets both of these. Give Animal a printSelf method that prints out thefollowing:^ 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 callsuper with the other two arguments. Write a method in Cat called printSelf. It should print “I ama cat”, then call the superclass’ printSelf method.

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

16-9:^

Interfaces

Previously, we talked about abstract classes. They allow a superclass to specify the methods a subclasswill 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 Batthat inherits 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 let us specify which methods an object shouldrespond to, without specifying how they should respond. This provides polymorphism - each object responds to amethod in the appropriate way. A class can implement as many interfaces as it wants.

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

Interfaces in the JDK

Comparable Iterable Iterator Cloneable Readable and many, many more

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

Comparable

public^

class^ Cat

extends

Animal implements

Comparable

public^ int^ compareTo(Object

other)

if^ (!(other

instanceof

Cat))^

System.out.println("Can’t

compare

these!");

return^

}^ else^

{ int^ age2^ =

((Cat)other).age; if^ (age

<^ age2) return^

-1;else^ if^ (age

^ age2) return^

1;else return^

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

Exercise

Rewrite Shape to be an Interface that declares an area()method. Rewrite Circle and Rectangle to implement the Shapeinterface. Now have Rectangle and Circle also implement theComparable interface.^ They will need to implement the compareTo method.Have them compare areas.

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