Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Isn’t this redundant? ArrayList foo = new ArrayList ();

3 A whole new world! ArrayList foo = new ArrayList (); //yawn! Object foo = new ArrayList (); //mysterious!

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

5 What we will do today 1.extends 2.Object 3.abstract 4.interface/implements With Robots!

6 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 job BendingRobot myBender; ClampingRobot myClampingRobot;

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

8 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

9 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

10 Object – the superclass when you have no superclass

11

12 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 with Object The Superclass when you have no superclass Implements toString() hashCode() and equals(), among others Coming next: Abstract

13 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 Robot GenericRobot myRobot; But we have no code to put into that common method

14 We can make an abstract Superclass Now We can Have GenericRobot objects //doesn’t matter what kind of Robot GenericRobot 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 }

15 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

16 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 with Object 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 it Abstract 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

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

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

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

20 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

21 Summary 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 with Object 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 it Abstract 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

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


Download ppt "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."

Similar presentations


Ads by Google