SlideShare a Scribd company logo
1 of 18
Page 0Classification: Restricted
Selenium Training
OOPS
Page 1Classification: Restricted
Agenda
• Object Communication: Messages
• Constructors
• Memory Management in Java
• Java Destructors?
• Garbage Collector
• Methods
Page 2Classification: Restricted
Object Communication: Messages (1 of 2)
• Objects communicate by sending messages
• getMoneyTotal and getName are examples of messages that can be sent to
the person object, Jim
• Sending messages is the only way that objects can communicate
Jim, how much
money do you
have?
I have
$18.27
Page 3Classification: Restricted
Object Communication: Messages (2 of 2)
• Sending a message is a different concept than calling a function
• Calling a function indicates that you have identified the actual
implementation code that you want to run at the time of the function
call
• Sending a message is just a request for a service from an object; the
object determines what to do
• Different objects may interpret the same message differently
getMoneyTotal
Message
Sender,
Instructor
Target Person,
Jim
$18.27
Page 4Classification: Restricted
Constructors
• The class body contains at least one constructor, which is a method that
sets up a new instance of a class
• The method has the same name as the class
• Use the new keyword with a constructor to create instances of a class
BankAccount account = new BankAccount();
Class instantiation
Page 5Classification: Restricted
Memory Management in Java
• Since Java does not use pointers, memory addresses cannot be accidentally
or maliciously overwritten
• The problems inherent in user allocated and deallocated memory are
avoided, since the Java Virtual Machine handles all memory management
• Programmers do not have to keep track of the memory they allocate
from the heap and explicitly deallocate it
Page 6Classification: Restricted
public BankAccount(String name) {
setOwner(name);
}
BankAccount account = new BankAccount("Joe Smith");
Constructor use
Constructor
definition
More About Constructors
• Constructors are used to create and initialize objects
• A constructor always has the same name as the class it constructs (case-
sensitive)
• Constructors have no return type
Page 7Classification: Restricted
Default Constructors
• The constructor with no arguments is a default constructor
• The Java platform provides a default constructor only if you do not
explicitly define any constructor
• When defining a constructor, you should also provide a default constructor
Page 8Classification: Restricted
Overloading Constructors
• There may be any number of constructors with different parameters
• This is called overloading
• Constructors are commonly overloaded to allow for different ways of
initializing instances
BankAccount new_account = new BankAccount();
BankAccount known_account = new BankAccount(account_number);
BankAccount named_account = new BankAccount(“My Checking Account”);
Page 9Classification: Restricted
Constructor Example
• In a constructor, the keyword this is used to refer to other constructors in
the same class
...
public BankAccount(String name) {
super();
owner = name;
}
public BankAccount() {
this("TestName");
}
public BankAccount(String name, double initialBalance) {
this(name);
setBalance(initialBalance);
}
...
Page 10Classification: Restricted
Java Destructors?
• Java does not have the concept of a destructor for objects that are no
longer in use
• Deallocation of memory is done automatically by the JVM
• A background process called the garbage collector reclaims the memory
of unreferenced objects
• The association between an object and an object reference is severed
by assigning another value to the object reference, for example:
objectReference = null;
–An object with no references is a candidate for deallocation during
garbage collection
Page 11Classification: Restricted
Garbage Collector
• The garbage collector sweeps through the JVM’s list of objects periodically
and reclaims the resources held by unreferenced objects
• All objects that have no object references are eligible for garbage collection
• References out of scope, objects to which you have assigned null, and
so forth
• The JVM decides when the garbage collector is run
• Typically, the garbage collector is run when memory is low
• May not be run at all
• Unpredictable timing
Page 12Classification: Restricted
Working with the Garbage Collector
• You cannot prevent the garbage collector from running, but you can
request it to run soon
• System.gc();
• This is only a request, not a guarantee
• The finalize() method of an object will be run immediately before garbage
collection occurs
• This method should only be used for special cases (such as cleaning up
memory allocation from native calls) because of the unpredictability of
the garbage collector
• Things like open sockets, files, and so forth should be cleaned up during
normal program flow before the object is dereferenced
Page 13Classification: Restricted
public void debit(double amount) {
// Method body
// Java code that implements method behavior
}
parameter list
return
typeaccess
modifier
method
name
Methods
• Methods define how an object responds to messages
• Methods define the behavior of the class
• All methods belong to a class
Page 14Classification: Restricted
public void credit(double
amount) {
...
}
method
name
argument
type
signatu
re
Method Signatures
• A class can have many methods with the same name
• Each method must have a different signature
• The method signature consists of:
• The method name
• Argument number and types
Page 15Classification: Restricted
public void method1(){
int a = 0;
System.out.println(a); // outputs 0
method2(a);
System.out.println(a); // outputs 0
}
void method2(int a){
a = a + 1;
}
Method Parameters
• Arguments (parameters) are passed:
• By value for primitive types
• By object reference for reference types
• Primitive values cannot be modified when passed as an argument
Page 16Classification: Restricted
public void debit(double amount) {
if (amount > getBalance()) return;
setBalance(getBalance() - amount);
}
public String getFullName() {
return getFirstName() + " " + getLastName();
}
Returning from Methods
• Methods return at most one value or one object
• If the return type is void, the return statement is optional
• The return keyword is used to return control to the calling method
• There may be several return statements in a method; the first one
reached will be executed
Page 17Classification: Restricted
BankAccount account = new BankAccount();
account.setOwner("Smith");
account.credit(1000.0);
System.out.println(account.getBalance());
...
public void credit(double amount) {
setBalance(getBalance() + amount);
}
BankAccount method
Invoking Methods
• To call a method, use the dot “.” operator
• The same operator is used to call both class and instance methods
• If the call is to a method of the same class, the dot operator is not
necessary

More Related Content

What's hot

C++ training
C++ training C++ training
C++ training PL Sharma
 
Java Serialization
Java SerializationJava Serialization
Java Serializationimypraz
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Abdullah Jan
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserializationYoung Alista
 
Android App code starter
Android App code starterAndroid App code starter
Android App code starterFatimaYousif11
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
Tool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAMLTool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAMLNick Pruehs
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptBinu Paul
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NETAbhi Arya
 
Tool Development 04 - XML
Tool Development 04 - XMLTool Development 04 - XML
Tool Development 04 - XMLNick Pruehs
 

What's hot (20)

06.1 .Net memory management
06.1 .Net memory management06.1 .Net memory management
06.1 .Net memory management
 
Ajax
AjaxAjax
Ajax
 
C++ training
C++ training C++ training
C++ training
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Serialization/deserialization
Serialization/deserializationSerialization/deserialization
Serialization/deserialization
 
Week3
Week3Week3
Week3
 
Android App code starter
Android App code starterAndroid App code starter
Android App code starter
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Ios development
Ios developmentIos development
Ios development
 
C++ classes
C++ classesC++ classes
C++ classes
 
03class
03class03class
03class
 
Tool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAMLTool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAML
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced Javascript
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
 
Tool Development 04 - XML
Tool Development 04 - XMLTool Development 04 - XML
Tool Development 04 - XML
 
class c++
class c++class c++
class c++
 
Json
Json Json
Json
 

Similar to Session 09 - OOPS

Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxKavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfKavitaHegde4
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and ClassesMichael Heron
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingRatnaJava
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructorsAsif Shahzad
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with javaSujit Kumar
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorSunipa Bera
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxsasukeman
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxAshrithaRokkam
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionHashni T
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator OverloadingMichael Heron
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptxurvashipundir04
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 

Similar to Session 09 - OOPS (20)

Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Chap01
Chap01Chap01
Chap01
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
C++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptxC++ Constructor and Destructors.pptx
C++ Constructor and Destructors.pptx
 
Class
ClassClass
Class
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
 
4-OOPS.pptx
4-OOPS.pptx4-OOPS.pptx
4-OOPS.pptx
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptx
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 

More from SiddharthSelenium

Session 08 - Arrays and Methods
Session 08 - Arrays and MethodsSession 08 - Arrays and Methods
Session 08 - Arrays and MethodsSiddharthSelenium
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control StatementsSiddharthSelenium
 
Session 05 - Introduction to WebDriver - Part 02
Session 05 - Introduction to WebDriver - Part 02Session 05 - Introduction to WebDriver - Part 02
Session 05 - Introduction to WebDriver - Part 02SiddharthSelenium
 
Session 04 - Creating Test Suites in IDE
Session 04 - Creating Test Suites in IDESession 04 - Creating Test Suites in IDE
Session 04 - Creating Test Suites in IDESiddharthSelenium
 
Session 02 - Selenium IDE - Part 2
Session 02 - Selenium IDE - Part 2Session 02 - Selenium IDE - Part 2
Session 02 - Selenium IDE - Part 2SiddharthSelenium
 
Session 02 - Object Identification - Part 1
Session 02 - Object Identification - Part 1Session 02 - Object Identification - Part 1
Session 02 - Object Identification - Part 1SiddharthSelenium
 
Session 01 - Introduction to Selenium - Part 2
Session 01 - Introduction to Selenium - Part 2Session 01 - Introduction to Selenium - Part 2
Session 01 - Introduction to Selenium - Part 2SiddharthSelenium
 
Session 01 - Introduction to Automation - Part 1
Session 01 - Introduction to Automation - Part 1Session 01 - Introduction to Automation - Part 1
Session 01 - Introduction to Automation - Part 1SiddharthSelenium
 

More from SiddharthSelenium (9)

Session 08 - Arrays and Methods
Session 08 - Arrays and MethodsSession 08 - Arrays and Methods
Session 08 - Arrays and Methods
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control Statements
 
Session 06 - Java Basics
Session 06 - Java BasicsSession 06 - Java Basics
Session 06 - Java Basics
 
Session 05 - Introduction to WebDriver - Part 02
Session 05 - Introduction to WebDriver - Part 02Session 05 - Introduction to WebDriver - Part 02
Session 05 - Introduction to WebDriver - Part 02
 
Session 04 - Creating Test Suites in IDE
Session 04 - Creating Test Suites in IDESession 04 - Creating Test Suites in IDE
Session 04 - Creating Test Suites in IDE
 
Session 02 - Selenium IDE - Part 2
Session 02 - Selenium IDE - Part 2Session 02 - Selenium IDE - Part 2
Session 02 - Selenium IDE - Part 2
 
Session 02 - Object Identification - Part 1
Session 02 - Object Identification - Part 1Session 02 - Object Identification - Part 1
Session 02 - Object Identification - Part 1
 
Session 01 - Introduction to Selenium - Part 2
Session 01 - Introduction to Selenium - Part 2Session 01 - Introduction to Selenium - Part 2
Session 01 - Introduction to Selenium - Part 2
 
Session 01 - Introduction to Automation - Part 1
Session 01 - Introduction to Automation - Part 1Session 01 - Introduction to Automation - Part 1
Session 01 - Introduction to Automation - Part 1
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 

Session 09 - OOPS

  • 2. Page 1Classification: Restricted Agenda • Object Communication: Messages • Constructors • Memory Management in Java • Java Destructors? • Garbage Collector • Methods
  • 3. Page 2Classification: Restricted Object Communication: Messages (1 of 2) • Objects communicate by sending messages • getMoneyTotal and getName are examples of messages that can be sent to the person object, Jim • Sending messages is the only way that objects can communicate Jim, how much money do you have? I have $18.27
  • 4. Page 3Classification: Restricted Object Communication: Messages (2 of 2) • Sending a message is a different concept than calling a function • Calling a function indicates that you have identified the actual implementation code that you want to run at the time of the function call • Sending a message is just a request for a service from an object; the object determines what to do • Different objects may interpret the same message differently getMoneyTotal Message Sender, Instructor Target Person, Jim $18.27
  • 5. Page 4Classification: Restricted Constructors • The class body contains at least one constructor, which is a method that sets up a new instance of a class • The method has the same name as the class • Use the new keyword with a constructor to create instances of a class BankAccount account = new BankAccount(); Class instantiation
  • 6. Page 5Classification: Restricted Memory Management in Java • Since Java does not use pointers, memory addresses cannot be accidentally or maliciously overwritten • The problems inherent in user allocated and deallocated memory are avoided, since the Java Virtual Machine handles all memory management • Programmers do not have to keep track of the memory they allocate from the heap and explicitly deallocate it
  • 7. Page 6Classification: Restricted public BankAccount(String name) { setOwner(name); } BankAccount account = new BankAccount("Joe Smith"); Constructor use Constructor definition More About Constructors • Constructors are used to create and initialize objects • A constructor always has the same name as the class it constructs (case- sensitive) • Constructors have no return type
  • 8. Page 7Classification: Restricted Default Constructors • The constructor with no arguments is a default constructor • The Java platform provides a default constructor only if you do not explicitly define any constructor • When defining a constructor, you should also provide a default constructor
  • 9. Page 8Classification: Restricted Overloading Constructors • There may be any number of constructors with different parameters • This is called overloading • Constructors are commonly overloaded to allow for different ways of initializing instances BankAccount new_account = new BankAccount(); BankAccount known_account = new BankAccount(account_number); BankAccount named_account = new BankAccount(“My Checking Account”);
  • 10. Page 9Classification: Restricted Constructor Example • In a constructor, the keyword this is used to refer to other constructors in the same class ... public BankAccount(String name) { super(); owner = name; } public BankAccount() { this("TestName"); } public BankAccount(String name, double initialBalance) { this(name); setBalance(initialBalance); } ...
  • 11. Page 10Classification: Restricted Java Destructors? • Java does not have the concept of a destructor for objects that are no longer in use • Deallocation of memory is done automatically by the JVM • A background process called the garbage collector reclaims the memory of unreferenced objects • The association between an object and an object reference is severed by assigning another value to the object reference, for example: objectReference = null; –An object with no references is a candidate for deallocation during garbage collection
  • 12. Page 11Classification: Restricted Garbage Collector • The garbage collector sweeps through the JVM’s list of objects periodically and reclaims the resources held by unreferenced objects • All objects that have no object references are eligible for garbage collection • References out of scope, objects to which you have assigned null, and so forth • The JVM decides when the garbage collector is run • Typically, the garbage collector is run when memory is low • May not be run at all • Unpredictable timing
  • 13. Page 12Classification: Restricted Working with the Garbage Collector • You cannot prevent the garbage collector from running, but you can request it to run soon • System.gc(); • This is only a request, not a guarantee • The finalize() method of an object will be run immediately before garbage collection occurs • This method should only be used for special cases (such as cleaning up memory allocation from native calls) because of the unpredictability of the garbage collector • Things like open sockets, files, and so forth should be cleaned up during normal program flow before the object is dereferenced
  • 14. Page 13Classification: Restricted public void debit(double amount) { // Method body // Java code that implements method behavior } parameter list return typeaccess modifier method name Methods • Methods define how an object responds to messages • Methods define the behavior of the class • All methods belong to a class
  • 15. Page 14Classification: Restricted public void credit(double amount) { ... } method name argument type signatu re Method Signatures • A class can have many methods with the same name • Each method must have a different signature • The method signature consists of: • The method name • Argument number and types
  • 16. Page 15Classification: Restricted public void method1(){ int a = 0; System.out.println(a); // outputs 0 method2(a); System.out.println(a); // outputs 0 } void method2(int a){ a = a + 1; } Method Parameters • Arguments (parameters) are passed: • By value for primitive types • By object reference for reference types • Primitive values cannot be modified when passed as an argument
  • 17. Page 16Classification: Restricted public void debit(double amount) { if (amount > getBalance()) return; setBalance(getBalance() - amount); } public String getFullName() { return getFirstName() + " " + getLastName(); } Returning from Methods • Methods return at most one value or one object • If the return type is void, the return statement is optional • The return keyword is used to return control to the calling method • There may be several return statements in a method; the first one reached will be executed
  • 18. Page 17Classification: Restricted BankAccount account = new BankAccount(); account.setOwner("Smith"); account.credit(1000.0); System.out.println(account.getBalance()); ... public void credit(double amount) { setBalance(getBalance() + amount); } BankAccount method Invoking Methods • To call a method, use the dot “.” operator • The same operator is used to call both class and instance methods • If the call is to a method of the same class, the dot operator is not necessary