0% found this document useful (0 votes)
8 views

Object Oriented Programming 3

Uploaded by

sarakyuth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Object Oriented Programming 3

Uploaded by

sarakyuth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Object Oriented Programming 3

Author : François Néron


Cursus : EENG3
UE :
Version : 1.0
Date : 20/03/2023

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

 Access to a computer with the following software installed :


• Eclipse
• No-install version available from T:/Numerique/Java/Installer/InstallEclipse
• Follow instructions in the provided README file
• May be installed from www.eclipse.org
• Java Runtime Environment (JRE)
• Installer available at www.java.com/en/download/
• Included in latest versions of eclipse (but not the version available on T:/…)

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

• Behavior - Functions + setIP(addrIP : string) : void

• Defined in the class + getMAC() : 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;

Constructor interfaceName = name;


m_isUp = false; //interface is down at
initialisation
M_addrIP = ’’’’; //IP is undefined at first
}
 Syntax NetworkInterface
• Always named as the class - m_addrMAC : string
• May have any kind of parameters - m_addrIP : string
 Function - m_isUp : bool
• Initializes attributes + interfaceName : string
 Call syntax + NetworkInterface(name : string, addrMac : string)
string n = ’’someName’’; + getStatus() : bool
string m = ’’a mac adress’’; + toggleStatus() : void
NetworkInterface instance = + getIP() : string
new NetworkInterface(n,m);
+ setIP(addrIP : string) : void
 Default + getMAC() : string
• If no constructor is defined, an empty + sendFrame(frame : string, destMAC : string) : void
constructor with no arguments is
generated. + sendPacket(packet : string, destIP : string) : void
- findDestMac(destIP : string) : string

ecam.fr 25/03/2021 8
Instances
 Object is an instance of class NetworkInterface

• One iteration of a model - m_addrMAC : string

• Each has own data - m_addrIP : string


- m_isUp : bool
 The Class is the type of the + interfaceName : string
Object + NetworkInterface(name : string, addrMac : string)
NetworkInterface wireless = new NetworkInterface(’’wifi’’, ’’…’’); + getStatus() : bool
NetworkInterface ethernet = new NetwokInterface(’’eth0’’, ’’…’’); + toggleStatus() : void
print(wireless.interfaceName); + getIP() : string
print(ethernet.interfaceName); + setIP(addrIP : string) : void
+ getMAC() : string
Prints : + sendFrame(frame : string, destMAC : string) : void
wifi + sendPacket(packet : string, destIP : string) : void
eth0 - findDestMac(destIP : string) : string

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

 Access method : + interfaceName : string

ethernet.setIP(’’192.172.16.12’’); + NetworkInterface(name : string, addrMac : string)

ethernet.toggleStatus(); + getStatus() : bool

 Privacy + toggleStatus() : void


+ getIP() : string
• + : public (default for methods)
+ setIP(addrIP : string) : void
• Accessible directly
+ getMAC() : string
• - : private (default for attributes)
+ sendFrame(frame : string, destMAC : string) : void
• Can only be accessed by
methods on the object + sendPacket(packet : string, destIP : string) : void
- findDestMac(destIP : string) : string

ecam.fr 25/03/2021 11
LinkedList – the basics
 What is a LinkedList ?
• A Set of LinkedList Cells

 What is a LinkedList Cell ?


• A data element (the CONTENT of the CELL)
• A link to the next cell in the list

First element (head of list) Last element (tail of list) Empty

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

for(int n = 0; n < index; n++){ + getSize() : int


+ get(index : int) : T
previousCell = previousCell.getNext();
+ add(elem : T, index : int) : void
}
+ remove(index : int)
LinkedListCell<T> newCell = new
LinkedListCell<T>() LinkedListCell<T>
- m_nextCell : LinkedListCell<T>
newCell.setNext( previousCell.getNext() );
- m_content : T
previousCell.setNext( newCell );
+ LinkedListCell() : void
newCell.setContent( elem );
+ getNext() : LinkedListCell<T>
m_size += 1; + getContent() : T
} + setNext(nextCell : LinkedListCell<T>) : void
+ setContent(elem) : void
+ isTail() : bool

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

previousCell = previousCell.getNext(); + add(elem : T, index : int) : void

} + remove(index : int)

LinkedListCell<T> nextCell = previousCell.getNext().getNext(); LinkedListCell<T>


- m_nextCell : LinkedListCell<T>
previousCell.setNext(nextCell );
- m_content : T
m_size -= 1;
+ LinkedListCell() : void
}
+ getNext() : LinkedListCell<T>
+ getContent() : T
+ setNext(nextCell : LinkedListCell<T>) : void
+ setContent(elem) : void
You don’t handle this deletion in your code
+ isTail() : bool
You only change the link, Java automatically forgets things that are referenced nowhere

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

A class is a type of object, defining its structure


• What kind of links can different classes have?

 In the UML standard, we recognize 5 relationships :


• Dependency

• Association
• Aggregation
• Composition

• Inheritance

ecam.fr 25/03/2021 18
Dependency

 The loosest of links

 Class A Depending on Class B simply means that changes in B


might impact the behavior of A.

 This can be because of any other kind of link, or even simply if


class A has a method that uses a Class B object as parameter

ecam.fr 25/03/2021 19
Association

 Association is a simple link, where one class uses an other


• May or may not be reciprocal
• May have any cardinality

 The relationship is basically just the ability to send some sort of


message to one another (in code, it means having access to the
public attributes and methods of the associated object)

 Every time a Class A has an class B attribute, it is (at least)


association.

ecam.fr 25/03/2021 20
Aggregation / Composition

 We have already unknowingly been using Aggregation or


Composition previously.
 They are specific and directional kinds of associations.

 Every time a Class A has a Class B object (or a container of that


type, such as an array or list) as attribute and one could say :
• The Class B object(s) BELONG(S) to the Class A object - Aggregation
• The Class A object is Composed of the Class B object(s) - Composition

 The difference between the two is conceptual (meaning there is


no different syntax for them in JAVA, for instance), but has
implications

ecam.fr 25/03/2021 21
Aggregation vs Composition

A Aggregates B B Composes A

 On deletion of A, B still exists  On deletion of A, B is deleted

A can be in a valid state (ie A usually can’t make sense


make sense) with an “empty” with a null in place of the B
B attribute attribute

 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

 Class B inherits from class A IF :


• Objects of class B ARE also necessarily of class A

 If class B inherits from class A :


• Objects of class B have all the methods and attributes of objects of class A
• But they may have additional ones, and implement methods differently !

 Multiples classes can inherit from the same class


• A class can logically inherit from several classes also, but not all language
permit this, as conflicts may arise.

ecam.fr 25/03/2021 23
Inheritance - Examples

A human and a cat are mortal


• so a Class Human and a Class Cat could both inherit from a class Mortal
• They would implement a method die() which comes from Mortal

A boat and a car are both vehicles


• so a class Boat and a class Car could both inherit from a class Vehicle
• They would have an attribute content and a method move(), coming from
Vehicle

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)

 What is NOT (this) Java :


• Coffee
• A dance
• An Island
• A slug mafia boss a long time ago on a far away planet
• JavaScript (another programming language, for web pages, with nothing to do
with Java)

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

 Compiled Language VS Interpreted Language

Write code Write code

Code Code

Compilation Run (through an interpreter)

Compiled Code

Run (directly on machine)

ecam.fr 25/03/2021 28
Java Compilation

 Java is a compiled language (like C, C++, but unlike python)

 Java compilers produce « byte-code », which is then executed


not directly by machines, but by the Java Virtual Machine, which
runs on the machines.

 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

Written Written Written


code code code

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

Computation Computation Computation

ecam.fr 25/03/2021 30
Java : the necessary tools

 To COMPILE Code (= translate it to Byte-Code)


• You need a Compiler, also known as JDK (Java development kit)

 To RUN BYTE-CODE
• You need a JRE (JVM + librairies)

 As there exists several C compilers, there are several JDKs


• Not all developped by Oracle, like the Android SDK (for phone apps)

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)
• …

 Butspecific tools were made to help developers and are embedded in


what we call an IDE (Integrated Development Environment)
• Eclipse
• Netbeans
• Visual Studio
• Visual Studio Code
• IntelliJ
• …

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

 We use it in the following instructions


b = a;
a = 10;
print(b);

 What do we want to see printed ?


• 5 => a and b are Values
• 10 => a and b are References

ecam.fr 25/03/2021 34
Value type variables / Reference type variables
Name
Name Type
Type Link
Values : Value References :

• Simple • More complex


Data
• Fixed size in memory • Dynamic handling (Value)

• Once value is modified, • May modify value by linking


previous value is overwritten new data (a link is smaller than
big data, so the write is quicker)
• Every write requires to • But how to handle the
actually rewrite the whole unlinked data?
Value in memory

ecam.fr 3525/03/2021 35
Copy by Value / Copy by Reference

Name Name2 Name Name2


Data Data2
Type = Type Type = Type
(Value) (Value2)
Value Value2 Link Link2

Name Name
Type Type
Value Link

Name Name Name2


??Data?? Data2
Type Type Type
?(Value)? (Value2)
Value2 Link2 Link2

ecam.fr 3625/03/2021 36
In Java : Values/Reference

 In Java, EVERYTHING is an object…

 EXCEPT base types (int, char, bool…)


• Even though there exists a class equivalent for each base type

 This means everything except base types will be copied…


• BY REFERENCE !

ecam.fr 25/03/2021 37
Thank you
for your attention

School of engineering since 1900


40, montée Saint-Barthélemy - 69321 LYON cedex 05
Tél. : 04 72 77 06 00 - [email protected]

ecam.fr ecam.fr

You might also like