Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables as Remote Control

Similar presentations


Presentation on theme: "Variables as Remote Control"— Presentation transcript:

1 Variables as Remote Control
A useful memory aid used in Head First Java

2 A Variable is a Reference
Person p = new Person( ) object a reference for sending commands to object Person #clone() equals(Object) finalize() getClass() hashCode() toString() getName(): Str getBirthday() p buttons on remote control are methods

3 The Compiler decides what Buttons
Person p = _____?______ Compiler uses the declared type (Person) of variable to decide what buttons (methods) it has. Person Remote getBirthday() getName() equals(Object) Buttons inherited from Object. Every remote control has these. toString()

4 Invoking Methods Person p = new Person( )
#clone() equals(Object) finalize() getClass() hashCode() toString() getName(): Str getBirthday() Object #clone() equals(Object) finalize() getClass() hashCode() toString() equals p getClass toString At runtime, JVM invokes method of actual object. If a class overrides a method, the override is used.

5 Student extends Person
toString( ) getGpa( ) Person #clone() equals(Object) finalize() getClass() hashCode() toString() getBirthday() getName(): str Object #clone() equals(Object) finalize() getClass() hashCode() toString() class Student extends Person { public double getGpa() { } public String toString( ) { }

6 What Buttons Does p Have?
Person p = new Student( ); Person Remote What buttons go here? equals Buttons inherited from Object. Every remote control has these. toString()

7 What Buttons Does p Have?
Person p = new Student( ); Student has a getGpa method. Why is there no getGpa button? Person Remote getBirthday( ) getName( ) equals toString() buttons from Object, even though definition is changed.

8 Method Signature includes Parameter
Student toString( ) equals(Student) getGpa( ) Person equals(Object) getName( ) Object equals(Object) toString() etc. Override equals(Object) New methods class Student extends Person { public boolean equals( Student s ) // BAD IDEA public String toString( )

9 Which equals( ) is called?
Student toString( ) equals(Student) Person equals(Object) getValue( ) Object equals(Object) toString() etc. Student a = new Student(); Person b = new Student( ); //1. b.equals( a ) //2. a.equals( b ) Draw the remote control !

10 Another view of Inheritance
Student Person getName( ) getGpa( ) getName( ) equals( Object ) equals( Object ) equals(Student)

11 Object References Student Object obj = new Student();
obj.toString( ) ??? getGpa( ) getName( ) An "Object" remote control (reference) only knows the methods for object. equals( Object ) equals(Student ) obj

12 How to Access the Real object
Student Object obj = new Student() ??? how can we call getGpa() using obj ??? obj.getGpa() //error getGpa( ) getValue( ) equals( Object ) equals(Student) obj

13 Solution: use a cast // "Object" remote (reference) only has buttons
// for methods of the Object class Object obj = new Student(); obj.getGpa( ); // ERROR! // Cast it to a "Student" reference (remote). Student s = (Student)obj; // "Student" remote (reference) has all // the methods of Student class. s.getGpa( ); // OK!


Download ppt "Variables as Remote Control"

Similar presentations


Ads by Google