22
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

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

Embed Size (px)

Citation preview

Page 1: 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

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).

Page 2: 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

Isn’t this redundant?

ArrayList<String> foo = new ArrayList<String>();

Page 3: 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

A whole new world!

ArrayList<String> foo = new ArrayList<String>();

//yawn!Object foo = new ArrayList<String>();//mysterious!

Page 4: 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

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?

Page 5: 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

What we will do today

1. extends2. Object3. abstract4. interface/implements

With Robots!

Page 6: 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

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;

Page 7: 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

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.

Page 8: 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

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

Page 9: 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

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

Page 10: 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

Object – the superclass when you have no superclass

Page 11: 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
Page 12: 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

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

Page 13: 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

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

Page 14: 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

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 }}

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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();}

Page 18: 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

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 }}

Page 19: 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

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}

Page 20: 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

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

Page 21: 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

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

Page 22: 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

• 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.