Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
LECTURE 7: INHERITANCE CSC 212 – Data Structures.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Computer Science I Inheritance Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Programming in Java CSCI-2220 Object Oriented Programming.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSC Programming for Science Lecture 23: More on Function Parameters.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
OOP: Encapsulation &Abstraction
Lecture 7: Reusing Code & Interfaces
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Lecture 8: Polymorphism & Class Design
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 9 Inheritance and Polymorphism
Java – Inheritance.
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How much would one pay for 4 lock picking kits, 8 ropes, and 5 wire cutters?

Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How much would one pay for 4 lock picking kits, 8 ropes, and 5 wire cutters?  Nothing, they’d steal it.

Sharing Among Classes

Composition has-a  Used when there is “has-a” relationship has a  Student has a name has a  Full name has a first name has an  Car has an engine has an  Rectangle has an upper right vertex  Use a field to compose classes  So we would add name field to Student class  firstName field in FullName class  If must use data externally, add getters & setters

Inheritance Is-a  “Is-a” relationships implemented via inheritance is a  Automobile is a Vehicle is a  Car is a Vehicle is an  Truck is an Automobile is an  Car is an Automobile extends  Subclass extends superclass for inheritance extends  Automobile extends Vehicle extends  Truck extends Automobile extends  Car extends Automobile

extends extends Example extends extends extends public class Vehicle {…} public class Automobile extends Vehicle {…} public class Car extends Automobile {…} public class Truck extends Automobile {…} ObjectVehicleAutomobileCarTruck

What Gets Inherited And How?  Class inherits all members from superclass  Subclass can use them directly unless they are private  Use as if they were copied into class w/o copying

What Gets Inherited And How?

What About Constructors? "is-a"  Fields and methods inherited since have "is-a"  Car is NOT a Vehicle without same data & actions  Adding members allowed, since Car more specific  But we (almost) always have access to fields & methods  Only during instantiation are constructors used  Cannot use elsewhere so would they help subclass?  Name is specific & tied to superclass; how to use?

Inherit Such a Course Word  Superclass constructor still needed by subclass… is-a  Remember, subclass instance is-a superclass instance  Instantiating superclass, too, when creating subclass  Somehow need to initialize instances superclass part  … but constructors not inherited (in same way)

Constructors Chains  Java requires reusing superclass’s constructor  Already initializes fields in superclass  Provides us with way to maximize laziness  Allows sharing constructor code within class, too  Still want to avoid copying code  Often do identical work initializing fields  Constructors increase laziness through chaining

Chains Explained

 Chain constructor using this(…) or super(…)  Must be first command  Must be first command in constructor  Call constructor in same class using this(…)  super(…) calls superclass constructor  Will be executed just like method call  Must match types listed for the parameters  If calling super(), cannot violate access protection

About To Get a Little Bumpy

Chaining Constructors Required  Every Java constructor must be chained…  this(…) or super(…) can be used to do chaining  Class loops forever or will call superclass constructor  But why is requirement new to you this lecture?

Chaining Constructors Required  Every Java constructor must be chained…  this(…) or super(…) can be used to do chaining  Class loops forever or will call superclass constructor  But why is requirement new to you this lecture?

Implicit Chaining Sounds Dirty  If chaining not explicit made at constructor start  Call to super() added by Java as implicit call  Requires superclass constructor with no parameters… … which exists in Object (default superclass)  But this empty constructor may not be in your classes

Constructors Not Inherited  Constructors not inherited directly by subclass  Calling superclass constructor relies upon chaining  Only via chaining is constructor accessible in subclass  Trying to use superclass constructor for new illegal  Not all classes define constructor of their own  If not defined, automatically get one with no params  Implied chain used by this implicit constructor  To do this superclass must include usable constructor

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }`

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }`

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { text = type; color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { super(type); color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Stickers public class Sticker { protected String text; public Sticker(String words) { text = words; } public void printMe() { System.out.println(text); } } public class CSticker extends Sticker { private String color; public CSticker(String clr, String type) { super(type); color = clr; } public CSticker(String type) { this("blue", type); } public static void main(String[] args) { Sticker s = new Sticker(“boo”); CSticker cs = new CSticker(“hoo”); s.printMe(); cs.printMe(); } }

Overriding Methods  Subclass can redeclare inherited method  Overloaded when different signature used  Method is called overridden if signatures identical  Specialize method in subclass by overriding  Changed only for subclass & its subclasses  Original used by superclass & other classes  Actual method called determined by instance’s type

Polymorphism And Assignment is-an  Subclass instance is-an instance of superclass is-a  This makes logical sense: Car is-a Vehicle is-a  Cannot say Vehicle is-a Car ; Opposite is not true  Can assign from subclass to superclass variable  Subclass variable is-a  Subclass variable is-a superclass variable, remember  Can't assign from superclass to subclass variable  May not be reasonable; not all Vehicle s are Car s

Types in Java  In Java, use reference variables like silhouettes  Compiler uses variable's type to see what is legal  But (may) lose detail: this ignores instances type  Important distinction between these two types  Variable's type determines if code can compile  If code works (& method called) uses instance's type

Inheritance Example public class SuperClass { public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getMyString() { return “sub ” + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); System.out.println(super1.otherMethod()); SubClass sub2 = super1;

Inheritance Example public class SuperClass { public String getMyString() { return “SUPER”; } } public class SubClass extends SuperClass { public String getMyString() { return “sub ” + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); System.out.println(super1.otherMethod()); SubClass sub2 = super1;

Your Turn  Get into your groups and complete activity

For Next Lecture  Read GT2.3 for Monday  What are the two types of exceptions?  When do you use each of type of exception?  What is similar between baseball and exceptions?  How are exceptions handled in subclasses?  There is weekly assignment problem on Angel  Due by 5PM next Tuesday (via )  Will be graded using the provided JUnit tests