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.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Review for Exam 1 As you arrive…please get a handout.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Abstract classes and Interfaces. Abstract classes.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
Sections Inheritance and Abstract Classes
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Presentation transcript:

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

A whole new world! ArrayList foo = new ArrayList (); //yawn! Object foo = new ArrayList (); //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.extends 2.Object 3.abstract 4.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 job BendingRobot 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 with Object 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 Robot GenericRobot 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 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 }

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

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

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

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.