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 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
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.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
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.
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.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Inheritance and Access Control CS 162 (Summer 2009)
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.
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.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
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.
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.
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)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Programming in Java: lecture 7
Modern Programming Tools And Techniques-I
Lecture 7: Reusing Code & Interfaces
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Lecture 8: Polymorphism & Class Design
Road Map Inheritance Class hierarchy Overriding methods Constructors
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
Java – Inheritance.
Java Inheritance.
Object Oriented Programming
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  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

Example of an Object Hierarchy 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

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

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  Java adds implicit call to super() automatically  Superclass needs 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 must  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

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  Call method based upon 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.4 for Friday  What is an abstract class?  What is an abstract method?  What is an interface ?  Why would anyone use any of these?  There is weekly assignment problem on Angel  Due by 5PM Tuesday (via )  Problem #2 graded using the provided JUnit tests