Upload
tamsin-mcdaniel
View
215
Download
0
Tags:
Embed Size (px)
Citation preview
Inheritance
Only not the good kind of inheritance where you discover your Great Aunt left you a yacht. The bad kind that involves
obscure Java syntax.
As you arrive: Please grab a handout, start a new Jan30Classwork (or whatever) project in Eclipse, and add 3 empty classes named whatever your like to them (though I personally enjoy Pirate Ninja Robot…just sayin).
Isn’t this redundant?
ArrayList<String> foo = new ArrayList<String>();
A whole new world!
ArrayList<String> foo = new ArrayList<String>();
//yawn!Object foo = new ArrayList<String>();//mysterious!
In the Optional Textbook
• The stuff today will be out of Chapter 7 and 8
• These chapters are worth reading• I know that when I was a student I
never did the optional reading. But surely you can make an exception just this once. Pweeetly please?
What we will do today
1. extends2. Object3. abstract4. interface/implements
With Robots!
Different “classes” of robots with different functions
class BendingRobot
doBending()useElectricity()
class ClampingRobot
doClamping()useElectricity()
class EvilSantaRobot
doPunishNaughtyChildren()useElectricity()
Different slots that require particular types of robots
//construction jobBendingRobot myBender;ClampingRobot myClampingRobot;
New Upgraded Robots “extend” existing Robot classes
class BendingRobot{doBending()}
class GoldBendingRobot extends BendingRobot{
//implicitly can doBending()doSuperCoolBending();}
GoldBendingRobot goldBot = new GoldBendingRobot();goldBot.doBending();goldBot.doSuperCoolBending();
Upgraded Robots are called subclasses. They “inherit” from their superclass…the more basic robot.
Because Subclass Robots can do everything Existing Robots Can Do, They Can Fill the Same Slots
class BendingRobot
doBending()
class GoldBendingRobot extends BendingRobot
//implicitly can doBending()doSuperCoolBending();
BendingRobot someBender = new GoldBendingRobot();someBender.doBending();someBender.doSuperCoolBending(); //NOT ALLOWED
Summary of Extends
• When one class extends another, it gets all the functions and variables of that class
• The subclass can be used anyplace the superclass can.
• The subclass can even “override” functions in the superclass with their own code if they want
• For maximum flexibility, always use the most general type you can get away with
Coming next: Object
Object – the superclass when you have no superclass
Summary Thus far
Extends• When one class extends another, it gets all the functions and
variables of that class• Subclasses can be used anyplace the superclasses can.• The subclasses can even “override” functions in the superclass
with their own code if they want• For maximum flexibility, always use the most general type you
can get away withObject• The Superclass when you have no superclass• Implements toString() hashCode() and equals(), among others
Coming next: Abstract
There are some things all robots have in common
class BendingRobot
doBending()useElectricity()
class ClampingRobot
doClamping()useElectricity()
class EvilSantaRobot
doPunishNaughtyChildren()useElectricity()
We Might Think We Want a Common Superclass
//doesn’t matter what kind of RobotGenericRobot myRobot;
But we have no code to put into that common method
We can make an abstract Superclass
Now We can Have GenericRobot objects//doesn’t matter what kind of RobotGenericRobot myRobot = new BendingRobot();myRobot.beep();myRobot.useElectricity();GenericRobot otherBot = new GenericRobot(); //NOT ALLOWED
abstract class GenericRobot { public abstract void useElectricity(); public void beep() { System.out.println(“Beep!”); }}
class BendingRobot extends GenericRobot { //must implement useElectricity() public void useElectricity() { //code goes here }}
Summary of Abstract
• We can make an “abstract” superclass, and add “abstract” functions to it
• Any subclass of the abstract superclass must implement the abstract functions
• We can have variables of the type of the abstract superclass, but we cannot create objects of the abstract superclass (you can never use new)
• Very often used in assignments to require specific features
Summary Thus farExtends• When one class extends another, it gets all the functions and variables of that class• Subclasses can be used anyplace the superclasses can.• The subclasses can even “override” functions in the superclass with their own code
if they want• For maximum flexibility, always use the most general type you can get away withObject• The Superclass when you have no superclass• Implements toString() hashCode() and equals(), among others• We can make an “abstract” superclass, and add “abstract” functions to itAbstract• Any subclass of the abstract superclass must implement the abstract functions• We can have variables of the type of the abstract superclass, but we cannot create
objects of the abstract superclass (you can never use new)
Coming next: Interface/Implements
What if we have a abstract class with no code whatsoever?
abstract class Appliance { public abstract void useElectricity(); public abstract int getSerialNumber();}// this code is legal, but should be an // interface
In Java, that has a special name and syntax. It’s called an “interface”.
interface Appliance { //no need to use abstract here because //everything is abstract in an interface public void useElectricity(); public int getSerialNumber();}
You don’t “extend” and interface, you “implement” an interface. But it works the same.
interface Appliance { public void useElectricity(); public int getSerialNumber();}
class Toaster implements Appliance { public void useElectricity() { //code } public int getSerialNumber() { //code }}
You can extend and implement two different things. You can implement more than 1 interface. But you can only extend 1
thing.
interface Appliance { public void useElectricity(); public int getSerialNumber();}
class HomeRobot extends Robot implements Appliance { public void useElectricity() { //code } public int getSerialNumber() { //code } //we’ve implicity inherited beep() from our superclass}
Summary of Interface
• Similar to a superclass, but has no code whatsoever
• You “implement” an interface in the same way you “extend” a superclass
• You can implement any number of interfaces, but can only extend 1 superclass
SummaryExtends• When one class extends another, it gets all the functions and variables of that class• Subclasses can be used anyplace the superclasses can.• The subclasses can even “override” functions in the superclass with their own code
if they want• For maximum flexibility, always use the most general type you can get away withObject• The Superclass when you have no superclass• Implements toString() hashCode() and equals(), among others• We can make an “abstract” superclass, and add “abstract” functions to itAbstract• Any subclass of the abstract superclass must implement the abstract functions• We can have variables of the type of the abstract superclass, but we cannot create
objects of the abstract superclass (you can never use new)Interface/Implements• Similar to a superclass, but has no code whatsoever• You “implement” an interface in the same way you “extend” a superclass• You can implement any number of interfaces, but can only extend 1 superclass
• Write a set of classes that use extends, abstract, and 2 interfaces
• Your classes don’t have to make sense and the can be whatever you want. I personally recommend that you use Pirate Ninja and Robot but whatever.