1 / 22

Inheritance

Learn about extending classes and creating abstract classes in Java, and how they can be used to inherit functions and variables. Gain a deeper understanding of object-oriented programming.

fcorbin
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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<String> foo = new ArrayList<String>();

  3. A whole new world! ArrayList<String> foo = new ArrayList<String>(); //yawn! Object foo = new ArrayList<String>(); //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 • extends • Object • abstract • 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 BendingRobotmyBender; ClampingRobotmyClampingRobot;

  7. New Upgraded Robots “extend” existing Robot classes class GoldBendingRobot extends BendingRobot { //implicitly can doBending() doSuperCoolBending(); } class BendingRobot { doBending() } GoldBendingRobotgoldBot = 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 GoldBendingRobot extends BendingRobot //implicitly can doBending() doSuperCoolBending(); class BendingRobot doBending() BendingRobotsomeBender = 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. 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

  12. 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 GenericRobotmyRobot; But we have no codeto put into that common method

  13. We can make an abstract Superclass 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 } } Now We can Have GenericRobot objects //doesn’t matter what kind of Robot GenericRobotmyRobot = new BendingRobot(); myRobot.beep(); myRobot.useElectricity(); GenericRobototherBot = new GenericRobot(); //NOT ALLOWED

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

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

  16. What if we have a abstract class with no code whatsoever? abstract class Appliance { public abstract void useElectricity(); public abstract intgetSerialNumber(); } // 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 intgetSerialNumber(); }

  17. You don’t “extend” and interface, you “implement” an interface. But it works the same. interface Appliance { public void useElectricity(); public intgetSerialNumber(); } class Toaster implements Appliance { public void useElectricity() { //code } public intgetSerialNumber() { //code } }

  18. 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 intgetSerialNumber(); } class HomeRobot extends Robot implements Appliance { public void useElectricity() { //code } public intgetSerialNumber() { //code } //we’ve implicity inherited beep() from our superclass }

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

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

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

More Related