COMP 110 Some notes on inheritance, review

Slides:



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

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.
Searching and Sorting I 1 Searching and Sorting 1.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
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.
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 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.
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.
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.
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 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java
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.
Modern Programming Tools And Techniques-I
Searching.
COMP 110 More arrays, 2D arrays, Program 4
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
CSC 205 Java Programming II
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
CS 302 Week 11 Jim Williams, PhD.
Non-static classes.
CSE 331 Software Design and Implementation
Inheritance 2nd Lecture
Testing, cont., Equality and Identity
COMP 110 Information hiding and encapsulation
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
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.
Administration, Coverage, Review
Effective Java, Chapter 3, 3rd Edition: Methods Common to All Objects
Overriding methods from the Object class: equals, toString
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Searching.
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Monday, May 20 Reminders: Final/Semester Exam for everyone this Wednesday, May 22 Last Friday (5-17) - Electricity Unit Test Test and lab make-up is today.
Announcements Program 1 due noon Lab 1 due noon
Tuesday, May 21 Reminders:
Review for Midterm 3.
Michele Weigle - COMP 14 - Spr 04
Chapter 7 Java Object Model
CS 240 – Advanced Programming Concepts
Presentation transcript:

COMP 110 Some notes on inheritance, review Michele Weigle - COMP 14 - Spr 04 COMP 110 Some notes on inheritance, review Catie Welsh April 25, 2011

Michele Weigle - COMP 14 - Spr 04 Announcements Program 4 due Wed, April 27th by 11:59pm Final exam, comprehensive Friday, May 6th, 12pm Review on Wednesday

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

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

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

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Revisiting the equals method Array and inheritance review

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

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

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

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

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

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

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

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;

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;

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

Array and Inheritance review worksheet Turn in for participation points for the day Will be returned to you on Wednesday

Wednesday Final exam review Come prepared with questions!