Top Banner
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. [email protected]
23

© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

© 2003 Walter Savitch

These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003.

They may not be copied or used for any other purpose without the written permission of Walter Savitch.

[email protected]

Page 2: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Midterm Warning

• In lecture Tuesday February 4.

• Quiz grades are not so great.

• Pick up graded quizzes from TA.

• Do Self-Test Exercises.

• Sample midterm on web page and in public.

• Can bring notes on one index card.

Page 3: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

You Must Have an Approved Picture ID at the Midterm or You

Get Zero• Only the following will do• A UCSD Picture ID• A Current Picture Drivers License• A Passport• Other ID approve (in person not via email) by

instructor at least 48 hours before the midterm

Page 4: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Inheritance

• Make new class (derived class) from an old class (base class).

• Derived class automatically has (inherits) all the methods and instance variables of the base class.

• Derived class normally also adds instance variables and/or methods.

Page 5: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

A (Base) Classpublic class Person{ private String name;

public Person( ) { name = "No name yet.";} public Person(String initialName) { name = initialName; }

Page 6: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

public void setName(String newName) { name = newName;}

public String getName( ) { return name;}

public void writeOutput( ) { System.out.println("Name: " + name);}

public boolean sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.name);}}

Page 7: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

A Derived Classpublic class Student extends Person{ private int studentNumber; public Student( ) { super( ); //Invokes base class constructor //to initialize namme instance variable studentNumber = 0 } public Student(String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; }

Page 8: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

A Derived Class Continued public void reset(String newName, int newStudentNumber) { setName(newName); studentNumber = newStudentNumber; }

public int getStudentNumber( ) { return studentNumber; }

public void setStudentNumber(int newStudentNumber) { studentNumber = newStudentNumber; }

Page 9: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

A Derived Class Continued public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

public boolean equals(Student otherStudent) { return (this.sameName(otherStudent) &&

(this.studentNumber == otherStudent.studentNumber)); }}

Page 10: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Private instance variables in the base class

• Are inherited by the derived class.

• Cannot be accessed by name in the definition of the derived class.

• Must be handled using public accessor and mutator methods inherited from the base class.

Page 11: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Overriding a Method Definition

In a derived class, if you include a method definition that has the same name and the exact same number and types of parameters as a method already in the base class, then for the derived class, this new definition replaces the old definition of the method.

Page 12: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

public class Person{ private String name;

public void writeOutput( ) { System.out.println("Name: " + name); }

Page 13: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

public class Student extends Person{ private int studentNumber;

public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

Page 14: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Student s = new Student();

… s.writeOutput( ); //uses the def. in Student, // not the one in Person

Page 15: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Overriding Versus Overloading

• If a method defined in the derived class has the exact same parameter list as a method of the same name in the base class, that is overriding. In the derived class the definition of the method is changed.

• If a method defined in the derived class does not have the exact same parameter list as a method of the same name in the base class, that is overloading. The derived class has both methods.

Page 16: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Call to an Overridden Method

public class Student extends Person{ private int studentNumber;

public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

Page 17: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Call to an Overridden Method

public class Student extends Person{ private int studentNumber;

public void writeOutput( )//Alternative def.{ super.writeOutput( ); //uses def. in Person System.out.println("Student Number: “ + studentNumber);}

Page 18: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

The Class Object

In Java, every class is a descendent of the

predefined class Object

Page 19: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

public class Species{ private String name; private int population; private double growthRate;

…MEANS

Page 20: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

MEANS:public class Species extends Object{ private String name; private int population; private double growthRate;

Page 21: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

Methods in the Class Object

• equals

• clone

• toString

Page 22: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

equals Methodpublic boolean equals(Object other)

Page 23: © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any.

public boolean equals(Object otherObject){ if (otherObject == null) return false; else if (!(otherObject instanceof Student)) return false; else { Student otherStudent = (Student)otherObject; return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); } }