03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf ·...
Transcript of 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf ·...
![Page 1: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/1.jpg)
Object-Oriented ProgrammingJava Specifics
03—Object-oriented Programming in Java
CS1102S: Data Structures and Algorithms
Li Hongyang and Martin Henz
January 21, 2009
Generated on Thursday 21st January, 2010, 11:13
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 1
![Page 2: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/2.jpg)
Object-Oriented ProgrammingJava Specifics
03—Object-oriented Programming in Java
CS1102S: Data Structures and Algorithms
Li Hongyang and Martin Henz
January 21, 2009
Generated on Thursday 21st January, 2010, 11:13
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 2
![Page 3: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/3.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Knowledge Representation View of Objects
Aggregation/Components
Classification/Instantiation
Generalization/Specialization
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 3
![Page 4: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/4.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Aggregation/Components
Data items can be grouped into bigger ones, whilepreserving their functionality
Example:
class Scol lab leTextArea {S c r o l l b a r x s c r o l l b a r ;S c r o l l b a r y s c r o l l b a r ;TextArea tex ta rea ;. . .
}
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 4
![Page 5: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/5.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Classification/Instantiation
Data structures (objects) are distinguished from theirclasses
Objects are created using a new operator on classes
class Vehic le {publ ic Vehic le ( i n t maxSpeed) { . . . }
}. . .Veh ic le myVehicle = new Vehic le ( 1 2 5 ) ;end
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 5
![Page 6: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/6.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Generalization/Specialization
Add/modify behavior by inheriting a class and (re-)definingmethods
Example:
class DecoratedWindow extends Window {void draw ( ) {
super . draw ( ) ;drawDecorat ion ( ) ;
}. . .
}
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 6
![Page 7: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/7.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Object-oriented Programming
Unifies three composition dimensions into a coherentsoftware methodology
Aggregation/ComponentsClassification/InstantiationGeneralization/Specialization
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 7
![Page 8: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/8.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Object-oriented Programming
Unifies three composition dimensions into a coherentsoftware methodology
Aggregation/ComponentsClassification/InstantiationGeneralization/Specialization
SIMULA was developed in mid 1960s in Norway
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 8
![Page 9: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/9.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Object-oriented Programming
Unifies three composition dimensions into a coherentsoftware methodology
Aggregation/ComponentsClassification/InstantiationGeneralization/Specialization
SIMULA was developed in mid 1960s in Norway
Concepts of SIMULA used in Smalltalk (1980s)
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 9
![Page 10: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/10.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Object-oriented Programming
Unifies three composition dimensions into a coherentsoftware methodology
Aggregation/ComponentsClassification/InstantiationGeneralization/Specialization
SIMULA was developed in mid 1960s in Norway
Concepts of SIMULA used in Smalltalk (1980s)
C++ (1980s), Java (1990s)
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 10
![Page 11: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/11.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Control Flow in Procedural Languages (time)P1 P2 P3
control
time
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 11
![Page 12: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/12.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Control Flow in Object-oriented Languages (time)
applicationmethodself orobject application
control
M3M2M1M3M2M1methods
O2O1objects
time
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 12
![Page 13: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/13.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Execution of Object Application
window . move ( 100 , 50 )
code
lookupmethod
lookupclass
argumentsidentifiermethodobject
execution
codeaddress
class
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 13
![Page 14: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/14.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Early Binding in Procedural Languages
procedureapplication
procedureexecution
executingcode
earlybinding
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 14
![Page 15: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/15.jpg)
Object-Oriented ProgrammingJava Specifics
Knowledge Representation View of ObjectsRuntime View
Late Binding in Object-oriented Languages
applicationobject
application"this"
executionmethod
bindinglate
"this"setting
methodmethod
bindingearly
application execution
executingcode
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 15
![Page 16: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/16.jpg)
Object-Oriented ProgrammingJava Specifics
Overriding of MethodsMethod DispatchingJava “Specialty”: Co-variance of Array Types
Overriding of Methods
In Java, a method can be “overridden”, if the parametertypes are exactly the same.Example:
c lass A {p u b l i c m(C c ) {
System . out . p r i n t l n ( ” A ” ) ;} }c lass B {
p u b l i c m(C c ) {System . out . p r i n t l n ( ” B ” ) ;
} }. . .B myObject = new B ( ) ;myObject .m(new C ( ) ) ;
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 16
![Page 17: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/17.jpg)
Object-Oriented ProgrammingJava Specifics
Overriding of MethodsMethod DispatchingJava “Specialty”: Co-variance of Array Types
Method Dispatching
Method dispatching uses:
the class of the actual objectthe descriptor of the method, consisting of:
the name of the methodthe types of the arguments
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 17
![Page 18: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/18.jpg)
Object-Oriented ProgrammingJava Specifics
Overriding of MethodsMethod DispatchingJava “Specialty”: Co-variance of Array Types
Example
c lass A {p u b l i c i n t m( Person P) {
System . out . p r i n t l n ( ” 1 ” ) ;} }c lass B extends A {
p u b l i c i n t m( Student P) {System . out . p r i n t l n ( ” 2 ” ) ;
} }B myB = new B ( ) ;Person p = new Student ( ) ;myB.m( p ) ; / / p r i n t s 1
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 18
![Page 19: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/19.jpg)
Object-Oriented ProgrammingJava Specifics
Overriding of MethodsMethod DispatchingJava “Specialty”: Co-variance of Array Types
Java “Specialty”: Co-variance of Array Types
(see page 15)
Person [ ] a r r = new Employee [ 5 ] ;a r r [ 0 ] = new Student ( . . . ) ;
This program compiles, but
produces a runtime error ArrayStoreException
CS1102S: Data Structures and Algorithms 03—Object-oriented Programming in Java 19
![Page 20: 03---Object-oriented Programming in Javacs1102s/slides/slides_cc_03.color.pdf · 03—Object-oriented Programming in Java CS1102S: Data Structures and Algorithms Li Hongyang and Martin](https://reader033.fdocuments.in/reader033/viewer/2022042221/5ec741021231d239d701eaea/html5/thumbnails/20.jpg)