Variables as Remote Control

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java Implementation: Part 3S CompSci 230 Software Construction.
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.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Peyman Dodangeh Sharif University of Technology Fall 2014.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
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.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism in Methods
Advanced Programming in Java
Lecture 17: Polymorphism (Part II)
Continuing Chapter 11 Inheritance and Polymorphism
Advanced Programming in Java
CS 302 Week 11 Jim Williams, PhD.
Lecture 3: Extending Classes Inheritance in Java
Overloading and Constructors
Polymorphism.
Extending Classes.
Polymorphism and access control
Chapter 9 Inheritance and Polymorphism
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Lecture 4: Extending Classes
Sampath Kumar S Assistant Professor, SECE
Barb Ericson Georgia Institute of Technology Oct 2005
Introduction to Inheritance
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Presentation transcript:

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

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

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()

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.

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( ) { . . . }

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()

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.

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( )

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 !

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

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

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

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!