Object Oriented Programming 3
Object Oriented Programming 3
ecam.fr
Introduction
ecam.fr 25/03/2021 2
Course Plan
Lectures (3*2h)
Tutorial Sessions (3*4h, split in 2 2h Practical each)
Final Evaluated Project
Goals :
• Understanding the basics of Object Oriented Programming
• Class, Object, Instance, Field/Attribute, Method, Scope, Constructor, Encapsulation
and Accessors, Inheritance
• Learn Programming in Java, an Object Oriented Language
• Using Eclipse
Contact Information :
• François Néron ([email protected])
ecam.fr 25/03/2021 3
Necessary Material
ecam.fr 25/03/2021 4
Getting ready for Java – Homework
If
you have an issue with the install, do not hesitate to contact me
BEFORE THE NEXT COURSE !
• You are HIGHLY encouraged to test your install by doing the first batch of
exercises before first tutorial, available on moodle and at :
T:/Numerique/Java/Exercices/en-SDA-Ex02.pdf
• To start having the coding habits which will be mandatory for the Project,
read and take into account the “Coding Style” document on moodle.
ecam.fr 25/03/2021 5
Reminders of Lesson 1&2
ecam.fr 25/03/2021 6
Class - Objects
NetworkInterface
Class = type of objects - m_addrMAC : string
• Class specifies structure of - m_addrIP : string
Attributes / Methods - m_isUp : bool
Attributes + interfaceName : string
• Data - Variables + NetworkInterface(name : string, addrMac : string)
• Type and name defined in class + getStatus() : bool
• Value in each object + toggleStatus() : void
Methods + getIP() : string
• Acts upon data in the specific + sendFrame(frame : string, destMAC : string) : void
object on which it is called + sendPacket(packet : string, destIP : string) : void
- findDestMac(destIP : string) : string
ecam.fr 25/03/2021 7
NetworkInterface (string name, string addrMac) {
m_addrMac = addrMac;
ecam.fr 25/03/2021 8
Instances
Object is an instance of class NetworkInterface
ecam.fr 25/03/2021 9
Instances
NetworkInterface
NetworkInterface wireless =
- m_addrMAC : string
new NetworkInterface(’’wifi’’, ’’ 5E:FF:56:A2:AF:16’’);
- m_addrIP : string
NetworkInterface ethernet =
- m_isUp : bool
new NetwokInterface(’’eth0’’, ’’ 5E:FF:56:A2:AF:15’’);
+ interfaceName : string
wireless.setIP(’’192.168.15.25’’);
+ NetworkInterface(name : string, addrMac : string)
ethernet.setIP(’’192.172.16.12’’);
+ getStatus() : bool
ethernet.toggleStatus();
+ toggleStatus() : void
+ getIP() : string
wireless ethernet
+ setIP(addrIP : string) : void
m_addrMac : ’’ 5E:FF:56:A2:AF:16’’ m_addrMac : ’’ 5E:FF:56:A2:AF:15’’
+ getMAC() : string
m_addrIP : ’’192.168.15.25’’ m_addrIP : ’’192.172.16.12’’
+ sendFrame(frame : string, destMAC : string) : void
m_isUp : false m_isUp : true
+ sendPacket(packet : string, destIP : string) : void
interfaceName : ’’wifi’ interfaceName : ’’eth0’’
- findDestMac(destIP : string) : string
ecam.fr 25/03/2021 10
Encapsulation - Accessors
NetworkInterface
Access data/Attribute/Field : - m_addrMAC : string
wireless.interfaceName; - m_addrIP : string
ethernet.interfaceName; - m_isUp : bool
ecam.fr 25/03/2021 11
LinkedList – the basics
What is a LinkedList ?
• A Set of LinkedList Cells
ecam.fr 25/03/2021 12
LinkedList vs Array
Array :
• Fixed Length
• Quick access to element from any index
• Slow adding/deleting/reordering
Both :
• Ordered set of same-type elements. Slow search.
LinkedList :
• Variable Length
• Access length proportional to depth of index
• Quick Adding/Deleting, EVEN in the middle of the list. Quick reordering.
ecam.fr 25/03/2021 13
LinkedList methods – add LinkedList<T>
- m_head : LinkedListCell<T>
public void add(elem : T, index : int){ - m_size : int
LinkedListCell<T> previousCell = m_head; + LinkedList() : void
ecam.fr 25/03/2021 14
LinkedList methods – remove LinkedList<T>
- m_head : LinkedListCell<T>
- m_size : int
public void remove(index : int){ + LinkedList() : void
LinkedListCell<T> previousCell = m_head; + getSize() : int
for(int n = 0; n < index; n++){ + get(index : int) : T
} + remove(index : int)
ecam.fr 25/03/2021 15
Correction of Home Exercise Batch 1
ecam.fr 25/03/2021 16
Advanced OOP :
Links between Classes - Inheritance
ecam.fr 25/03/2021 17
Links between classes
• Association
• Aggregation
• Composition
• Inheritance
ecam.fr 25/03/2021 18
Dependency
ecam.fr 25/03/2021 19
Association
ecam.fr 25/03/2021 20
Aggregation / Composition
ecam.fr 25/03/2021 21
Aggregation vs Composition
A Aggregates B B Composes A
Example : Example :
• The list of aliens coming from a • The rooms of a house
planet • The cells of a LinkedList
ecam.fr 25/03/2021 22
Inheritance
ecam.fr 25/03/2021 23
Inheritance - Examples
ecam.fr 25/03/2021 24
Java : An Object-Oriented
Programming Language
ecam.fr 25/03/2021 25
What is Java ?
What is Java ?
• A compiled, object-oriented, strongly typed programming language created
by Sun/Oracle (company)
• A virtual machine (the Java Runtime Environment)
ecam.fr 25/03/2021 26
The intention behind Java
Java: http://www.oracle.com/technetwork/java/index-
136113.html
• It must be :
• “simple, object oriented, and familiar”
• “robust and secure”
• “architecture-neutral and portable”
• “high performance”
• “interpreted, threaded, and dynamic”
ecam.fr 25/03/2021 27
Compilation
Code Code
Compiled Code
ecam.fr 25/03/2021 28
Java Compilation
This means that any Java code is portable to any machine able
to run the JVM (aka JRE, Java Runtime Environment)
ecam.fr 25/03/2021 29
Java Compilation
Java Compiler
Compiler
Interpreter Byte-code
Compiled
(Software running
code
live on machine)
JVM
(Software running
Directly
Machine Machine live on machine)
executable
specific specific
ecam.fr 25/03/2021 30
Java : the necessary tools
To RUN BYTE-CODE
• You need a JRE (JVM + librairies)
ecam.fr 25/03/2021 31
Java : coding
Source code for Java is basicly just text, you may write it with any text
editor, even the most basic :
• Notepad
• Vi / Emacs
• Word (saving to .txt, then changing extension)
• …
ecam.fr 25/03/2021 32
Memory and Objects : References
ecam.fr 25/03/2021 33
Value / Reference
a
We have a variable int
5
ecam.fr 25/03/2021 34
Value type variables / Reference type variables
Name
Name Type
Type Link
Values : Value References :
ecam.fr 3525/03/2021 35
Copy by Value / Copy by Reference
Name Name
Type Type
Value Link
ecam.fr 3625/03/2021 36
In Java : Values/Reference
ecam.fr 25/03/2021 37
Thank you
for your attention
ecam.fr ecam.fr