

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


?? Department of Computer Science — University of San Francisco – p.1/
?? Department of Computer Science — University of San Francisco – p.2/
Person class public
lastName; String public id; String public
?? Department of Computer Science — University of San Francisco – p.3/
Person extends Professor class public
officeNum; String public
?? Department of Computer Science — University of San Francisco – p.4/
lname; = lastName
?? Department of Computer Science — University of San Francisco – p.5/
= lname; lastName office; = officeNum
?? 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()
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 /
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 /
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.
fly(); void public
flying!’’); System.out.println(‘‘I’m
?? Department of Computer Science — University of San Francisco – p.12/