Instance Methods and Inheritance (1/2)

Similar documents
Inheritance 11/02/2015. Department of Computer Science & Information Engineering. National Taiwan University

Class versus Instance (Section 5.1)

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

Inheritance. Hsuan-Tien Lin. OOP Class, March 29-30, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Inheritance OOP 03/29-30/ / 41

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

Generics. Hsuan-Tien Lin. OOP Class, May 23, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Generics OOP 05/23/ / 16

Array 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

More on Methods and Encapsulation

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

Encapsulation 10/26/2015. Department of Computer Science & Information Engineering. National Taiwan University

Polymorphism 11/09/2015. Department of Computer Science & Information Engineering. National Taiwan University

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

1 Java Night Countdown (30%)

CMSC 132, Object-Oriented Programming II Summer Lecture 1:

Machine Learning Foundations

COMP 204. Object Oriented Programming (OOP) - Inheritance. Mathieu Blanchette

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

Lab Exercise 6 CS 2334

SDS developer guide. Develop distributed and parallel applications in Java. Nathanaël Cottin. version

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

Lecture 5: Jun. 10, 2015

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

Python. chrysn

Android Services. Lecture 4. Operating Systems Practical. 26 October 2016

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Machine Learning Foundations

Lecture 5: Sep. 23 &25

Java Programming. Final Examination on December 13, 2015 Fall 2015

Machine Learning Techniques

Machine Learning Techniques

Lecture 14: Nov. 11 & 13

Machine Learning Foundations

Extensibility Patterns: Extension Access

Machine Learning Techniques

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

ITI Introduction to Computing II

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Machine Learning Foundations

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Machine Learning Techniques

Information System Design IT60105

A JML Specification of the Design Pattern Visitor

Object Oriented Software Design (NTU, Class Even, Spring 2009) Final Examination Problem Sheet TIME: 06/16/2009, 14:20 17:20

Factory method - Increasing the reusability at the cost of understandability

Machine Learning Techniques

public void run ( ) { i f ( this. id == 0) System. out. p r i n t ( 3 ) ; bro. j o i n ( ) ; else System. out. p r i n t ( 2 ) ;

ITI Introduction to Computing II

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

ECE 571 Advanced Microprocessor-Based Design Lecture 10

Classroom Tips and Techniques: Series Expansions Robert J. Lopez Emeritus Professor of Mathematics and Maple Fellow Maplesoft

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 09/06/2014 SESSION 8:30-10:30

JACKIE AND THE HORRIBLE FAMILY TRAVAUX PEDAGOGIQUES AVANT-SPECTACLE. -Copyright by Koalako-

Binary Search Trees. Motivation

CHAPTER 01. Introduction. 3DBrushwork.com. P a g e 2

Lab Course: distributed data analytics

Machine Learning Techniques

CS106A: Programming Methodologies. Piech, CS106A, Stanford University

CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

P2 Topic 4 - Motion and forces Higher

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

Name: SAMPLE EOC PROBLEMS

SSJ User s Guide. Package randvarmulti Generating Random Vectors

MATHEMATICAL OBJECTS in

Ligand Scout Tutorials

Introduction to Digital Evolution Handout Answers

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

CS 3411 Systems Programming

INF Models of concurrency

Constructors - Cont. must be distinguished by the number or type of their arguments.

Cognitive Systems 300: Probability and Causality (cont.)

Part I. System call overview. Secure Operating System Design and Implementation System Calls. Overview. Abstraction. Jon A. Solworth.

Machine Learning Techniques

Binary Relation Review Questions

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Files and their Relationships

Doubles. Doubles. Doubles. Doubles. Doubles. Doubles. Doubles. Doubles. Doubles DOUBLES

Speculative Parallelism in Cilk++

MI-RUB Testing Lecture 10

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Activities, Fragments and Intents

6.001 Recitation 22: Streams

Unless provided with information to the contrary, assume for each question below that the Classical Linear Model assumptions hold.

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

SAMPLE SAMPLE SAMPLE

AlgAE (Algorithm Animation Engine) Reference Manual Version 3.0. Steven J. Zeil Old Dominion University Dept. of Computer Science

More with Systems of Equations

Climate Change. Presenter s Script

Techniques of Java Programming

ECE 201 Fall 2009 Final Exam

6th Grade. Translating to Equations. Slide 1 / 65 Slide 2 / 65. Slide 4 / 65. Slide 3 / 65. Slide 5 / 65. Slide 6 / 65

Programsystemkonstruktion med C++: Övning 1. Karl Palmskog Translated by: Siavash Soleimanifard. September 2011

ALGEBRA 1 FINAL EXAM 2006

6/25/14. The Distribution Normality. Bell Curve. Normal Distribution. Data can be "distributed" (spread out) in different ways.

WEATHER ON WHEELS Middle School Program

1. Write a program to calculate distance traveled by light

AP* Physics B: Newton s Laws YOU MAY USE YOUR CALCULATOR

Arithmetic Properties w. A = lw Additive Inverse: a + (ˉa) = 0 l 1 Multiplicative Inverse: a = 1 a

Computer Science Introductory Course MSc - Introduction to Java

Transcription:

Instance Methods and Inheritance (1/2) 1 class Professor { 2 p u b l i c void say_hello ( ) { 3 System. out. p r i n t l n ( " Hello! " ) ; 4 } 5 } 6 class CSIEProfessor extends Professor { 7 p u b l i c void play_bbs ( ) { 8 System. out. p r i n t l n ( " Fun! " ) ; 9 } 10 } CSIEProfessor: two instance methods; Professor: one instance method Hsuan-Tien Lin (NTU CSIE) 20/41

Instance Methods and Inheritance (2/2) 1 class Professor { 2 p u b l i c void say_hello ( ) { 3 System. out. p r i n t l n ( " Hello! " ) ; 4 } 5 } 6 class CSIEProfessor extends Professor { 7 p u b l i c void say_hello ( ) { 8 System. out. p r i n t l n ( "May the OOP course be with you! " ) ; 9 } 10 } 11 / a l t e r n a t i v e s 12 CSIEProfessor HTLin = new CSIEProfessor ( ) ; 13 Professor HTLin = new CSIEProfessor ( ) ; 14 Professor HTLin = new Professor ( ) ; 15 / 16 / c a l l s 17 HTLin. say_hello ( ) ; 18 / which say_hello() will be called? Hsuan-Tien Lin (NTU CSIE) 21/41

Instance Methods and Inheritance: Key Point instance method binding: dynamic, depending on run-time instance types Hsuan-Tien Lin (NTU CSIE) 22/41

Reference Assignment and Inheritance (1/4) 1 class Student { i n t ID ; S t r i n g name ; } 2 class GodStudent extends Student { Award [ ] president_awards ; } 3 4 p u b l i c class StudentDemo { 5 p u b l i c s t a t i c show_student_id ( Student s ) { 6 System. out. p r i n t l n ( s. ID ) ; 7 } 8 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 9 GodStudent h t l i n = new GodStudent ( ) ; 10 show_student_id ( h t l i n ) ; 11 Student CharlieL = new Student ( ) ; 12 show_student_id ( CharlieL ) ; 13 } 14 } htlin refers to an instance of GodStudent (for sure!) htlin refers to an instance of Student as well one instance, many coherent types (in argument passing, return value, etc.) Hsuan-Tien Lin (NTU CSIE) 23/41

Reference Assignment and Inheritance (2/4) 1 class Student { i n t ID ; S t r i n g name ; } 2 class GodStudent extends Student { Award [ ] president_awards ; } 3 4 p u b l i c class StudentDemo { 5 p u b l i c s t a t i c void main ( S t r i n g [ ] argv ) { 6 GodStudent h t l i n = new GodStudent ( ) ; 7 / / I want to be a usual student 8 Student usualstudent = h t l i n ; 9 Student anotherusualstudent = new Student ( ) ; 10 } 11 } if copying assignment, a copy of htlin can be a usual student but reference assignment in Java, how can htlin be usual? mechanism? Hsuan-Tien Lin (NTU CSIE) 24/41

Reference Assignment and Inheritance (3/4) 1 class Student { i n t ID ; S t r i n g name ; } 2 / / takes 4 + ( 8 ) bytes 3 class GodStudent extends Student { Award [ ] president_awards ; } 4 / / takes 4 + ( 8 ) + ( 8 ) bytes 5 6 / excluding some other i n f o r m a t i o n, much l i k e 7 class GodStudent { 8 / / f i r s t 4 + ( 8 ) bytes ( f o r Student ) 9 i n t ID ; 10 S t r i n g name ; 11 / / l a s t ( 8 ) bytes ( f o r GodStudent ) 12 Award [ ] president_awards ; 13 } / / takes 4 + ( 8 ) + ( 8 ) bytes 14 / one possible mechanism for single inheritance (Java!!): shared prefix (virtually) Hsuan-Tien Lin (NTU CSIE) 25/41

Reference Assignment and Inheritance (4/4) 1 class Student { i n t ID ; S t r i n g name ; } 2 / / class Student { i n t 000; reference 004; } 3 class GodStudent extends Student { Award [ ] president_awards ; } 4 / / class GodStudent { i n t 000; reference 004; reference 012; } one possible mechanism: variable name a single number when class loads (after instance type check) objects can just safely access memory contents by numbers Hsuan-Tien Lin (NTU CSIE) 26/41

Reference Assignment and Inheritance: Key Point a simple run-time mechanism (shared prefix): ancestor first, descendant last Hsuan-Tien Lin (NTU CSIE) 27/41

Constructor and Inheritance (1/3) 1 class Student { i n t ID ; S t r i n g name ; 2 Student ( i n t ID, S t r i n g name ) { t h i s. ID = ID ; t h i s. name = name ; } 3 } 4 class AwardStudent extends Student { 5 Award [ ] awards ; 6 AwardStudent ( i n t ID, S t r i n g name, i n t naward ) { 7 super ( ID, name ) ; / / means invoke Student ( ID, name ) ; 8 awards = new Award [ naward ] ; 9 } 10 } initialize the ancestor part first construct AwardStudent (i.e. calls) construct Student thus, the Student parts of the memory are initialized first, then the AwardStudent part Hsuan-Tien Lin (NTU CSIE) 28/41

Constructor and Inheritance (2/3) 1 class Student { i n t ID ; S t r i n g name ; 2 Student ( i n t ID, S t r i n g name ) { t h i s. ID = ID ; t h i s. name = name ; } 3 } 4 class AwardStudent extends Student { 5 Award [ ] awards ; 6 AwardStudent ( i n t ID, S t r i n g name, i n t naward ) { 7 super ( ID, name ) ; / / means invoke Student ( ID, name ) ; 8 / / any u t i l i t y f u n c t i o n i n Student can be used at t h i s stage 9 awards = new Award [ naward ] ; 10 } 11 } super goes first so that there is a valid Student object in the very beginning Hsuan-Tien Lin (NTU CSIE) 29/41

Constructor and Inheritance (3/3) 1 class Student / extends Object / { 2 i n t ID ; S t r i n g name ; 3 Student ( i n t ID, S t r i n g name ) { t h i s. ID = ID ; t h i s. name = name ; } 4 / 5 Student ( i n t ID, S t r i n g name ) { 6 super ( ) ; / / l i k e Object ( ) ; 7 t h i s. ID = ID ; t h i s. name = name ; 8 } 9 / 10 } 11 class AwardStudent extends Student { 12 Award [ ] awards ; 13 AwardStudent ( i n t ID, S t r i n g name, i n t naward ) { 14 / /? 15 } 16 } super() automatically added if no explicit call in the beginning Hsuan-Tien Lin (NTU CSIE) 30/41

A Fallback: Constructor Calls 1 class Record { 2 S t r i n g name ; i n t score ; 3 Record ( i n t i n i t _ s c o r e ) { score = i n i t _ s c o r e ; } 4 Record ( ) { Record ( 4 0 ) ; } 5 } 6 p u b l i c class RecordDemo { 7 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 8 Record r1 = new Record ( 6 0 ) ; 9 Record r2 = new Record ( ) ; 10 System. out. p r i n t l n ( r1. score ) ; 11 System. out. p r i n t l n ( r2. score ) ; 12 } 13 } there is a bug above one constructor can only call one other constructor (via this or super) Hsuan-Tien Lin (NTU CSIE) 31/41

Constructor and Inheritance: Key Point calls ancestor constructor first (and thus ancestor forms first) Hsuan-Tien Lin (NTU CSIE) 32/41

Private Variables and Inheritance (1/1) 1 class Parent { 2 p r i v a t e i n t hidden_money ; 3 p u b l i c void show_hidden_money_amount ( ) { } 4 } 5 class Child extends Parent { 6 void spend_money ( ) { 7 / / can hidden_money be spent here? 8 / / can show_hidden_money_amount ( ) be c a l l e d here? 9 } 10 } (1) not directly (2) yes does Child have a hidden_money slot in the memory? yes, to make show_hidden_money_amount() work! yes, to make the shared prefix mechanism work! Hsuan-Tien Lin (NTU CSIE) 33/41

Private Variables and Inheritance: Key Point private variables are still inherited in memory, but not visible to the subclass because of encapsulation Hsuan-Tien Lin (NTU CSIE) 34/41

Private Methods and Inheritance (1/1) 1 class Parent { 2 p r i v a t e i n t hidden_money ; 3 p r i v a t e void b u y _ l i q u o r ( ) { } 4 p u b l i c void show_hidden_money_amount ( ) { } 5 } 6 class Child extends Parent { 7 void buy ( ) { 8 / / can b u y _ l i q u o r ( ) be c a l l e d here? 9 } 10 } no, because cannot see private methods effectively not inherited Hsuan-Tien Lin (NTU CSIE) 35/41

Private Methods and Inheritance: Key Point private methods effectively not inherited because not visible to the subclass Hsuan-Tien Lin (NTU CSIE) 36/41

More on Access Permissions (1/2) 1 package generation. old ; 2 class Parent { 3 p r i v a t e i n t hidden_money ; 4 p u b l i c void show_hidden_money_amount ( ) { } 5 / d e f a u l t / void middle_age_issues ( ) ; 6 / d e f a u l t / void cross_gen_issues ( ) ; 7 } 8 / / d i f f e r e n t f i l e, Child. java 9 package generation. new ; 10 class Child extends Parent { 11 } in Child, can hidden_money be accessed? no. can show_hidden_money_amount be accessed? yes. can middle_age_issues be accessed? no. can cross_gen_issues be accessed? no. what if we want yes? Hsuan-Tien Lin (NTU CSIE) 37/41

More on Access Permissions (2/2) 1 package generation. old ; 2 p u b l i c class Parent { 3 p r i v a t e i n t hidden_money ; 4 p u b l i c void show_hidden_money_amount ( ) { } 5 / d e f a u l t / void middle_age_issues ( ) ; 6 protected void cross_gen_issues ( ) ; 7 } 8 / / d i f f e r e n t f i l e, Child. java 9 package generation. new ; 10 class Child extends Parent { 11 } can cross_gen_issues be accessed? no. what if we want yes? protected: accessible to Child (sub-classes) and Friend (same-package-classes) Hsuan-Tien Lin (NTU CSIE) 38/41

More on Access Permissions: Key Point public: accessible to everyone protected: accessible to sub-classes and same-package-classes (default): accessible to same-packageclasses private: accessible within my class definitions Hsuan-Tien Lin (NTU CSIE) 39/41

Permissions and Method Overriding (1/1) 1 p u b l i c class Parent { 2 protected void method ( ) { } 3 } 4 class Child extends Parent { 5 p u b l i c void method ( ) { } / /? 6 p r i v a t e void method ( ) { } / /? 7 } 8 9 / / bottom l i n e 10 Parent var = new Child ( ) ; 11 var. method ( ) ; need last two lines to work for things same-package as Parent need last two lines to reflect overriding (calling Child s) Child: same or more open than Parent Hsuan-Tien Lin (NTU CSIE) 40/41

Permissions and Method Overriding: Key Points Child: same or more open than Parent Hsuan-Tien Lin (NTU CSIE) 41/41