Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 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.

Similar presentations


Presentation on theme: "© 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."— Presentation transcript:

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 other purpose without the written permission of Walter Savitch. wsavitch@ucsd.edu

2 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.

3 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

4 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.

5 A (Base) Class public class Person { private String name; public Person( ) { name = "No name yet.";} public Person(String initialName) { name = initialName; }

6 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);} }

7 A Derived Class public 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; }

8 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; }

9 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)); }

10 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.

11 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.

12 public class Person { private String name; public void writeOutput( ) { System.out.println("Name: " + name); }

13 public class Student extends Person { private int studentNumber; public void writeOutput( ) { System.out.println("Name: " + getName( ) ); System.out.println("Student Number: " + studentNumber); }

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

15 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.

16 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); }

17 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); }

18 The Class Object In Java, every class is a descendent of the predefined class Object

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

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

21 Methods in the Class Object equals clone toString

22 equals Method public boolean equals(Object other)

23 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)); } }


Download ppt "© 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."

Similar presentations


Ads by Google