Comp 110 some notes on inheritance review
Download
1 / 18

COMP 110 Some notes on inheritance, review - PowerPoint PPT Presentation


  • 144 Views
  • Uploaded on

COMP 110 Some notes on inheritance, review. Catie Welsh April 25, 2011. Announcements. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive Friday, May 6th, 12pm Review on Wednesday. Questions?. Final exam. Final exam will cover everything we have covered Lectures

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' COMP 110 Some notes on inheritance, review' - delila


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Comp 110 some notes on inheritance review

COMP 110Some notes on inheritance, review

Catie Welsh

April 25, 2011


Announcements
Announcements

  • Program 4 due Wed, April 27th by 11:59pm

  • Final exam, comprehensive

    • Friday, May 6th, 12pm

    • Review on Wednesday



Final exam
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
Final exam review on Wednesday

  • No formal review

  • Look over previous assignments, lectures, midterm, etc.

  • Come prepared with questions


Today in comp 110
Today in COMP 110

  • Revisiting the equals method

  • Array and inheritance review


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

    objectinstanceofClass_Name

  • Use this operator in the equals method


The equals method
The equals method

  • Third try

    publicboolean equals(Object obj)

    {

    if ((obj != null) && (obj instanceof Student))

    {

    Student otherStudent = (Student) obj;

    return (this.id == otherStudent.id);

    }

    returnfalse;

    }

  • 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 method1
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 method2
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 method3
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 method4
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 method5
The equals method

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


The equals method6
The equals method

  • This implementation is not symmetric

    publicboolean equals(Object obj)

    {

    if ((obj != null) && (obj instanceof Student))

    {

    Student otherStudent = (Student) obj;

    return (this.id == otherStudent.id);

    }

    returnfalse;

    }


The equals method7
The equals method

  • Why?

    • The instanceof operator will return true if obj is a subclass of Student

      publicboolean equals(Object obj)

      {

      if ((obj != null) && (obj instanceof Student))

      {

      Student otherStudent = (Student) obj;

      return (this.id == otherStudent.id);

      }

      returnfalse;

      }


The equals method8
The equals method

  • Fourth try

    publicboolean equals(Object obj)

    {

    if (this == obj)

    returntrue;

    if ((obj == null) || (obj.getClass() != this.getClass()))

    returnfalse;

    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
Array and Inheritance review worksheet

  • Turn in for participation points for the day

    • Will be returned to you on Wednesday


Wednesday
Wednesday

  • Final exam review

  • Come prepared with questions!


ad