Catie Welsh April 18, 2011.  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
1 SSD3 - Unit 2 Java toString & Equals Presentation Class Website:
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
COMP Inheritance Basics Yi Hong June 09, 2015.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
Classes Revisited Chapter 8.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Modern Programming Tools And Techniques-I
Polymorphism.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
COMP 110 More about inheritance
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
COMP 110 Some notes on inheritance, review
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
CSE 143 Lecture 22 The Object class; Polymorphism read
Chapter 9: Polymorphism and Inheritance
CSE 143 Lecture 22 The Object class; Polymorphism read
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Java Inheritance.
Polymorphism.
Lecture 18: Polymorphism (Part II)
Chapter 9 Carrano Chapter 10 Small Java
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
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
Advanced Programming in Java
Presentation transcript:

Catie Welsh April 18, 2011

 Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday 2

3

 Inheritance and polymorphism 4

 Person has a jump method, so all subclasses have a jump method 5 Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Each subclass has its own jump functionality public class Person { public void jump() { System.out.println("Whee!"); } public class Athlete extends Person { public void jump() { System.out.println("I jump really well!"); } 6

 ExtremeAthlete is an Athlete  XGamesSkater is a Person  Person is not necessarily a Skydiver 7 Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

 Person p = new ExtremeAthlete(); ◦ legal  Athlete a = new Athlete(); ◦ legal  XGamesSkater xgs = new Person(); ◦ illegal 8

 “many forms” Enables the substitution of one object for another as long as the objects have the same interface 9

public static void jump3Times(Person p) { p.jump(); } public static void main(String[] args) { XGamesSkater xgs = new XGamesSkater(); Athlete ath = new Athlete(); jump3Times(xgs); jump3Times(ath); } 10

 Note that we wrote the class Person before any of the derived classes were written  We can create a new class that inherits from Person, and the correct jump method will be called because of dynamic binding 11

 The method invocation is not bound to the method definition until the program executes public class SkiJumper extends ExtremeAthlete { public void jump() { System.out.println("Launch off a ramp and land on snow"); } } public static void main(String[] args) { SkiJumper sj = new SkiJumper(); jump3Times(sj); } 12

 Every class in Java is derived from the class Object ◦ Every class in Java is an Object 13 Animal ReptileMammal HumanCrocodileWhale Object Person StudentEmployee

 Object has several public methods that are inherited by subclasses  Two commonly overridden Object methods: ◦toString ◦equals 14

 There is a version of System.out.println that takes an Object as a parameter. What happens if we do this? Person p = new Person(); System.out.println(p);  We get something like:  The class hash code 15

 Every class has a toString method, inherited from Object public String toString()  Intent is that toString be overridden, so subclasses can return a custom String representation 16

 the object’s toString method is called  the String that is returned by the toString method is printed 17 public class Person { private String name; public Person(String name) { this.name = name; } public String toString() { return "Name: " + name; } } public class Test { public static void main(String[] args) { Person per = new Person("Apu"); System.out.println(per); } } Output: Name: Apu

(Assume the Person class has a getName method) public class Student extends Person { private int id; public Student(String name, int id) { super(name); this.id = id; } public String toString() { return "Name: " + getName() + ", ID: " + id; } public class Test { public static void main(String[] args) { Student std = new Student("Apu", 17832); System.out.println(std); } 18 Output: Name: Apu, ID: 17832

 Would this compile? public class Test { public static void main(String[] args) { Person per = new Student("Apu", 17832); System.out.println(per); } }  Yes. What is the output?  Automatically calls Student’s toString method because per is of type Student 19 Output: Name: Apu, ID: 17832

 First try: public boolean equals(Student std) { return (this.id == std.id); }  However, we really want to be able to test if two Objects are equal 20

 Object has an equals method ◦ Subclasses should override it public boolean equals(Object obj) { return (this == obj); }  What does this method do? ◦ Returns whether this has the same address as obj ◦ This is the default behavior for subclasses 21

 Second try public boolean equals(Object obj) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); }  What does this method do? ◦ Typecasts the incoming Object to a Student ◦ Returns whether this has the same id as otherStudent 22

public boolean equals(Object obj) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); }  Why do we need to typecast? ◦ Object does not have an id, obj.id would not compile  What’s the problem with this method? ◦ What if the object passed in is not actually a Student? ◦ The typecast will fail and we will get a runtime error 23

 We can test whether an object is of a certain class type: if (obj instanceof Student) { System.out.println("obj is an instance of the class Student"); }  Syntax: object instanceof Class_Name  Use this operator in the equals method 24

 Third try public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; }  Reminder: null is a special constant that can be assigned to a variable of a class type – means that the variable does not refer to anything right now 25

 Get into groups of 3-4 ◦ At least 1 person you have never worked with before ◦ At least 1 video game player  Design 3-4 video game characters ◦ Brainstorm instance variables and methods for each ◦ What is common between the characters? ◦ How could you use inheritance to take advantage what is in common? What would your inheritance hierarchy look like?  Write your names down and hand it in 26

 Search Algorithms 27