Inheritance Based on slides by Ethan Apter

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
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.
CSC 142 Computer Science II Zhen Jiang West Chester University
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Some Object-Oriented Programming (OOP) Review. Let’s practice writing some classes Write an Employee class with methods that return values for the following.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Inheritance ndex.html ndex.htmland “Java.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
1 Interacting with the superclass (continued) suggested reading:9.4.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 16: Polymorphism (Part I)
Lecture 5:Interfaces and Abstract Classes
Lecture 25: Inheritance and Polymorphism
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Building Java Programs Chapter 9
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 9
Inheritance and Polymorphism
Lecture 15: More Inheritance
Lecture 9-2: Interacting with the Superclass (super);
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Designing for Inheritance
Chapter 19 Generics Dr. Clincy - Lecture.
Inheritance Basics Programming with Inheritance
CSE 143 Lecture 22 The Object class; Polymorphism read
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
slides created by Ethan Apter
CSE 143 Lecture 22 The Object class; Polymorphism read
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Computer Programming with JAVA
slides created by Ethan Apter
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Building Java Programs
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Topic 31 - inheritance.
Building Java Programs
Inheritance Readings: 9.1.
CMSC 202 Generics.
Lecture 17: Polymorphism (Part I)
Lecture 15: Inheritance II
Chapter 14 Abstract Classes and Interfaces
Building Java Programs
Polymorphism 2019/4/29.
Building Java Programs
Building Java Programs
CSE 143 Lecture 23 Polymorphism; the Object class read
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Building Java Programs
slides created by Ethan Apter and Marty Stepp
Building Java Programs
Building Java Programs
Presentation transcript:

Inheritance Based on slides by Ethan Apter CSE 143 Lecture 15 Inheritance Based on slides by Ethan Apter

Intuition: Employee Types Consider this (partial) hierarchy of employee types: What kind of tasks can each employee type perform? Employee Clerical Professional Secretary Legal Secretary Lawyer Engineer

Intuition: Employee Types What tasks should all employees be able to do? show up for work work collect paychecks What tasks can a lawyer do that an engineer cannot? sue file legal briefs Which kind of secretary (regular or legal) can accomplish a greater variety of tasks? legal secretaries can do all regular secretarial work and have special training for additional tasks

Intuition: Employee Training On your first day at work, you’ll likely receive some general training for your new job If it’s a big company, you’ll likely receive this with many other types of employees engineers, business people, lawyers, etc After this general training, you’ll probably receive some specialized training “I know yesterday they told you to fill out your time-card on the white sheet, but here we do it online instead” We call this kind of replacement overriding the new behavior overrides/replaces the old behavior

Inheritance Overview Java does something similar with inheritance If we want to show an inheritance relationship between two classes, we use the extends keyword: public class B extends A { ... } Which sets up the following inheritance hierarchy: A B superclass subclass

Superclasses and Subclasses In the previous example, A is the superclass of B (A is above B on the hierarchy), and B is a subclass of A (B is below A on the hierarchy) This wording is somewhat different from standard English: So, a “super bacon cheeseburger” is just a hamburger that doesn’t seem right: it’s missing bacon and cheese! but that’s how inheritance works superclass subclass hamburger bacon cheeseburger

Base and Derived Classes We also say A is the base class of B, and B is a derived class of A This makes a little more sense: A hamburger provides the basic form of a bacon cheeseburger. Alternately, a bacon cheeseburger is a hamburger with minor additions hamburger bacon cheeseburger base class derived class

Extending Object Consider the class A that we’ve been discussing: public class A { ... } We didn’t write that A extends anything, but it automatically extends Object: All the classes you’ve written so far extend Object This diagram is more complete than before. However, we often don’t draw Object because we know it must be there A B Object

Object Object is a very general class Since every class must extend Object, either directly like A or indirectly like B, Object must have only state and behavior that is needed by every class: equals toString this is where the weird default toString comes from and more (but we won’t bother with the others)

Why Use Inheritance? Inheritance allows us to reuse code we’ve already written this makes it a powerful tool Inheritance also allows us to express the core relationship between different classes Subclasses allow us to do two things: add new state and behavior (fields and methods) useful when a superclass has most of the needed state and behavior override inherited methods useful when you need to modify the way an inherited method functions

Substituting When can we substitute one object for another? Recall our employee hierarchy: most simple and generic most complex and specific Employee Clerical Professional Secretary Legal Secretary Lawyer Engineer

Substituting We can always substitute a more specific object for a less specific one. With inheritance, we call this an “is-a” relationship a lawyer is-a professional employee is-an employee a lawyer can substitute for an employee, etc a legal secretary is-a secretary is-a clerical employee is-an employee a legal secretary can substitute for a secretary, etc You can see the is-a relationship by moving up the inheritance hierarchy It’s not ok to substitute across the hierarchy a secretary is NOT a lawyer and can’t substitute for one

Substituting Recall our classes B and A (B extends A) Obviously we can do this: A x = new A(); B y = new B(); But what if the variable type and object type don’t match? A x = new B(); B y = new A(); variable type object type perfectly fine not good!

Substituting But, what does it mean when the variable type doesn’t match the object type? A x = new B(); We are limited to the behaviors of the variable type for x above, we are limited to using only methods defined for class A (which may be inherited from Object) When executed, the methods will behave as defined in the object type for x above, the methods will execute as defined in B (which may be inherited from A or Object)

Casting Suppose that: So far, this is fine (just not ideal) you’re an unemployed legal secretary you know that legal secretaries earn $20 an hour you know that generic secretaries earn $15 an hour you accept a job as a generic secretary for $15 an hour So far, this is fine (just not ideal) But what if: your employer discovers that you’re a legal secretary ...and wants you to do legal secretary work ...for just $15 an hour? Is that ok?

Casting No, it’s not ok! If he wants you to do legal secretary work, he can renegotiate your contract to reflect this and pay you $20 an hour Java lets us do something similar when we class cast the class cast essentially renegotiates the contract Secretary you = new LegalSecretary(); LegalSecretary youWithRaise = (LegalSecretary)you; cast

Exercise: Inheritance Mystery 4-5 classes with inheritance relationships are shown the class names won’t make sense (inheritance mystery) A client program calls methods on objects of each class some questions involve casting some lines of code are illegal and produce errors You must read the code and determine what it does if it produces output, you must be precise if it produces an error, you need to specify what kind of error (either a compiler error or a runtime error) A similar problem will be on your midterm!

Exercise: Inheritance Mystery Assume that the following classes have been declared: public class One { public void method1() { System.out.println("One1"); } public class Two extends One { public void method3() { System.out.println("Two3"); public class Three extends One { public void method2() { System.out.println("Three2"); method1(); public class Four extends Three { System.out.println("Four1"); super.method1(); System.out.println("Four3");

Technique: Diagramming ...and determine where methods are defined and inherited Object One method1 Two One’s method1 method3 Three One’s method1 method2 Four method1 Three’s method2 method3

Technique: Behavior Table Then, figure out the behaviors of each type of object Class method1 method2 method3 One Two Three Four

Statically Bound Method Calls Let’s look a little closer at Four’s method1(): public class Four extends Three { public void method1() { System.out.println("Four1"); super.method1(); } ... super is a Java keyword to look to the super class: so super.method1() is like saying Three.method1() super is statically bound: it always refers to Three’s methods

Dynamically Bound Method Calls Let’s look a little closer at Three’s method2(): public class Three extends One { public void method2() { System.out.println("Three2"); method1(); } Here the call on method1 is dynamic Which version of method1 should we call? This is determined at runtime Depends on the actual object type (not the declared type of the variable) that we end up calling the method on it always runs the “current” version of method1, because it’s possible that another subclass can redefine method1

Technique: Behavior Table Then, figure out the behaviors of each type of object Class method1 method2 method3 One One1 Two Two3 Three Three2 method1() Four Four1 Four3 Italics - inherited method is called Circled - dynamic method call

Exercise: Inheritance Mystery Assume that the following variables have been declared: One var1 = new Two(); One var2 = new Three(); One var3 = new Four(); Three var4 = new Four(); Object var5 = new Three(); Object var6 = new One();

Exercise: Inheritance Mystery What do the following lines of code do? var1.method1(); var2.method1(); var3.method1(); var4.method1(); var5.method1(); var6.method1(); var4.method2(); var4.method3();

Exercise: Inheritance Mystery What do the following lines of code do? ((Two)var1).method2(); ((Three)var1).method2(); ((Two)var1).method3(); ((Four)var2).method1(); ((Four)var3).method1(); ((Four)var4).method3();

Exercise: Inheritance Mystery What do the following lines of code do? ((One)var5).method1(); ((Four)var5).method2(); ((Three)var5).method2(); ((One)var6).method1(); ((One)var6).method2(); ((Two)var6).method3();

Solving Inheritance Mystery Steps to solving inheritance mystery: Look at the variable type (if there is a cast, look at the casted variable type). If the variable type does not have the requested method the compiler will report an error. If there was a cast, make sure the casted variable type is compatible with the object type (i.e. ensure the object type is a subclass of the variable type). If they are not compatible, a runtime error (ClassCastException) will occur. Execute the method in question, behaving like the object type (the variable type and casted variable type no longer matter at all)

Extra Example

Exercise: Inheritance Mystery 4-5 classes with inheritance relationships are shown the class names won’t make sense (inheritance mystery) A client program calls methods on objects of each class some questions involve casting some lines of code are illegal and produce errors You must read the code and determine what it does if it produces output, you must be precise if it produces an error, you need to specify what kind of error (either a compiler error or a runtime error) A similar problem will be on your midterm!

Exercise: Inheritance Mystery Assume that the following classes have been declared: public class Fog extends Sleet { public void method1() { System.out.println("Fog 1"); } public void method3() { System.out.println("Fog 3"); public class Rain extends Snow { System.out.println("Rain 1"); public void method2() { System.out.println("Rain 2");

Exercise: Inheritance Mystery public class Sleet extends Snow { public void method2() { System.out.println("Sleet 2"); super.method2(); method3(); } public void method3() { System.out.println("Sleet 3"); public class Snow { System.out.println("Snow 2"); System.out.println("Snow 3");

Technique: Diagramming First, determine the inheritance hierarchy: Object Snow Rain Sleet Fog

Technique: Diagramming ...and determine where methods are defined and inherited Object Snow method2 method3 Rain method1 (method3) Sleet Fog (method2)

Method Calls Let’s look a little closer at Sleet’s method2(): public class Sleet extends Snow { public void method2() { System.out.println("Sleet 2"); super.method2(); method3(); } ... super is a Java keyword to look to the super class so super.method2() is like saying Snow.method2() super is statically bound: it always refers to Snow’s methods however, the call on method3 is dynamic it always runs the current version of method3, because it’s possible that another subclass can redefine method3

Technique: Behavior Table Then, figure out the behaviors of each type of object method Snow Rain Sleet Fog method1 --- Rain 1 Fog 1 method2 Snow 2 Rain 2 Sleet 2 method3() method3 Snow 3 Sleet 3 Fog 3 Italics - inherited behavior Circled - dynamic method call

Exercise What happens when the following examples are executed? Example 1 (letter a on handout #15): Snow var1 = new Sleet(); var1.method3(); Example 2 (letter c on handout #15): var1.method2(); Example 3 (letter o on handout #15): Snow var3 = new Fog(); ((Sleet)var3).method1(); Example 4 (letter m on handout #15): ((Rain)var3).method1();

Example 1 Problem: Output: Snow var1 = new Sleet(); var1.method3(); variable Snow method2 method3 Rain method1 (method3) Sleet Fog (method2) object Sleet 3

Example 2 Problem: Output: Snow var1 = new Sleet(); var1.method2(); variable Snow method2 method3 Rain method1 (method3) Sleet Fog (method2) object Sleet 2 Snow 2 Sleet 3

Example 3 Problem: Output: Snow var3 = new Fog(); ((Sleet)var3).method1(); Output: Snow method2 method3 Rain method1 (method3) Sleet Fog (method2) variable No output! Compiler error! Sleet doesn’t have a method1()! object

Example 4 Problem: Output: Snow var3 = new Fog(); ((Rain)var3).method1(); Output: Snow method2 method3 Rain method1 (method3) Sleet Fog (method2) variable No output! Runtime error! A Fog is not a Rain! object

Solutions to Handout #15 Solutions for letters a through j: Letter Code Output a var1.method1(); compiler error b var2.method1(); Rain 1 c var1.method2(); Sleet 2/Snow 2/Sleet 3 d var2.method2(); Rain 2 e var3.method2(); Sleet 2/Snow 2/Fog 3 f var4.method2(); g var5.method2(); h var1.method3(); Sleet 3 i var2.method3(); Snow 3 j var3.method3(); Fog 3

Solutions to Handout #15 Solutions for letters k through t: Letter Code Output k var4.method3(); compiler error l var5.method3(); Fog 3 m ((Rain)var3).method1(); runtime error n ((Fog)var5).method1(); Fog 1 o ((Sleet)var3).method1(); p ((Sleet)var3).method3(); q ((Fog)var6).method3(); r ((Snow)var4).method2(); Snow 2 s ((Sleet)var4).method3(); t ((Rain)var6).method3(); Snow 3