









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Intro to Computer Science II; Subject: Computer Science; University: University of San Francisco (CA); Term: Unknown 1989;
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!
Department of 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.
Department of Computer Science — University of San Francisco – p. 2/
15-4:^ Example public^ class
Professor
extends
Person
String
officeNum; public^ void
teach()
public^ void
grade()
public^ void
forget()
Department of 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 classconstructors?
Department of Computer Science — University of San Francisco – p. 5/
15-7:^ 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/
15-8:^ 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/
15-10:^ 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/
15-11:^ 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/
15-13:^ 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/
15-14:^ Interfaces in the JDK^ •^ Comparable^ •^ Iterable^ •^ Iterator^ •^ Cloneable^ •^ Readable^ •^ and many, many more
Department of Computer Science — University of San Francisco – p. 14/
15-16:^ Comparable public^ class
Cat^ extends
Animal^
implements
Comparable
public^ int
compareTo(Object
other)
if^ (!(other
instanceof
Cat))^
System.out.println("Can’t
compare
these!");
=^ ((Cat)other).age; if^ (age^
< age2) return^ -1;else
if^ (age
age2)
Department of Computer Science — University of San Francisco – p. 16/
15-17:^ Exercise^ •^ Rewrite Shape to be an Interface that declares an area()method.^ •^ Rewrite Circle and Rectangle to implement the Shape interface.^ •^ Now have Rectangle and Circle also implement theComparable interface.^ ◦^
They will need to implement the compareTo method. Havethem compare areas.
Department of Computer Science — University of San Francisco – p. 17/