COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.

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 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Programo Issues Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009.
Effective Java, Chapter 3: Methods Common to All Objects.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
== equals ? CSE 331 SOFTWARE DESIGN & IMPLEMENTATION EQUALITY Autumn 2011.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
1 COMP 110 Inheritance Tabitha Peck M.S. April 16, 2008 MWF 3-3:50 pm Philips 367.
Searching and Sorting I 1 Searching and Sorting 1.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Equality CSE 331 University of Washington. Object equality A simple idea - we have intuitions about equality: - Two objects are equal if they have the.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 ==, equals(), and all that (Slides by David Notkin and Mike Ernst) 1.
1 TCSS 143, Autumn 2004 Lecture Notes Creating Java Classes.
DATA STRUCTURES Lecture: Polymorphism Slides adapted from Prof. Steven Roehrig.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Java Type System and Object Model Horstmann ch , 7.7.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public.
Searching.
CSC 205 Java Programming II
CS 302 Week 11 Jim Williams, PhD.
COMP 110 Some notes on inheritance, review
Non-static classes.
CSE 331 Software Design and Implementation
Inheritance 2nd Lecture
Testing, cont., Equality and Identity
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Effective Java, Chapter 3, 3rd Edition: Methods Common to All Objects
Overriding methods from the Object class: equals, toString
Searching.
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Chapter 8 Class Inheritance and Interfaces
Review for Midterm 3.
Chapter 7 Java Object Model
Presentation transcript:

COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Final exam, comprehensive ◦ Saturday, December 6, 4pm 2

Questions? 3

Final exam Final exam will cover everything we have covered ◦ Lectures ◦ Readings from textbook ◦ Labs ◦ Programs ◦ In-class exercises/worksheets ◦ Midterm 4

Final exam review on Wednesday No formal review Look over previous assignments, lectures, midterm, etc. Come prepared with questions 5

Today in COMP 110 Revisiting the equals method Array and inheritance review 6

The instanceof operator 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 7

The equals method 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 8

The equals method Implements an equivalence relation: It is reflexive ◦ For any non-null reference value x, x.equals(x) should return true 9

The equals method It is symmetric ◦ For any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true 10

The equals method It is transitive ◦ For any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true 11

The equals method It is consistent ◦ For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified 12

The equals method For any non-null reference value x, x.equals(null) should return false 13

The equals method This implementation is not symmetric public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; } Demonstrate in jGRASP 14

The equals method Why? ◦ The instanceof operator will return true if obj is a subclass of Student public boolean equals(Object obj) { if ((obj != null) && (obj instanceof Student)) { Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } return false; } Demonstrate problem in jGRASP 15

The equals method Fourth try public boolean equals(Object obj) { if (this == obj) return true; if ((obj == null) || (obj.getClass() != this.getClass())) return false; Student otherStudent = (Student) obj; return (this.id == otherStudent.id); } The getClass method will return the runtime class of an object, so if obj is a subclass of Student (in this case), this method will return false 16

COMP110 Battle … of Doom 17 Lazy Lenny Weapon/Special ability: sleep MagicalFlying amphibian Weapon/Special ability: fly, make smoke, disappear VS WINNER

COMP110 Battle … of Doom 18 Spring Weapon/Special ability: allergies MagicalFlying amphibian Weapon/Special ability: fly, make smoke, disappear VS WINNER It’s tough to fly when you are suffering from allergies!

COMP110 Battle … of Doom 19 Spring Weapon/Special ability: allergies StarburstMan Weapon/Special ability: _assaulted_by_starburst_ca.php _assaulted_by_starburst_ca.php VS WINNER

Array and Inheritance review worksheet 20

Wednesday Final exam review Come prepared with questions! 21