SlideShare a Scribd company logo
Object Oriented Prograring(OOP) java
2
Quetion::explain the five way of
creating object in java
● Different ways to create objects in
Java
As you all know, in Java, a class
provides the blueprint for objects,
you create an object from a class.
There are many different ways to
create objects in Java.
Following are some ways in which
you can create objects in Java:
3
1) Using new Keyword
● 1) Using new Keyword :
Using new keyword is the most basic
way to create an object. This is the
most common way to create an
object in java. Almost 99% of objects
are created in this way. By using this
method we can call any constructor
we want to call (no argument or
parameterized constructors).
4
5
2) Using New Instance
2) Using New Instance :
● If we know the name of the class & if it has a
public default constructor we can create an
object –Class.forName. We can use it to
create the Object of a Class. Class.forName
actually loads the Class in Java but doesn’t
create any Object. To Create an Object of the
Class you have to use the new Instance
Method of the Class.
6
7
3) Using clone() method
Using clone() method:
● Whenever clone() is called on any object,
the JVM actually creates a new object and
copies all content of the previous object
into it. Creating an object using the clone
method does not invoke any constructor.
● To use clone() method on an object we
need to implement Cloneable and define
the clone() method in it.
8
.
Note :
1)Here we are creating the clone of an existing Object and not any new Object.
2)Class need to implement Cloneable Interface otherwise
it will throw CloneNotSupportedException.
9
4) Using deserialization
4) Using deserialization :
● Whenever we serialize and then
deserialize an object, JVM creates a
separate object. In deserialization, JVM
doesn’t use any constructor to create the
object.
● To deserialize an object we need to
implement the Serializable interface in
the class.
10
Serializing an Object :
11
Deserialization of Object :
Object of DeserializationExample class is serialized
using writeObject() method and written to file.txt
file.
12
5 Factory Method:
●
Factory method is a creational design pattern, i.e, related to object creation. In
Factory pattern, we create object without exposing the creation logic to client and the
client use the same common interface to create new type of object. The idea is to use
a static member-function (static factory method) which creates & returns instances,
hiding the details of class modules from user.
●
A factory pattern is one of the core design principles to create an object, allowing
clients to create objects of a library(explained below) in a way such that it doesn’t
have tight coupling with the class hierarchy of the library.
WHAT IS MEANT WHEN WE TALK ABOUT LIBRARY AND CLIENTS?
● A library is something which is provided by some third party which exposes some
public APIs and clients make calls to those public APIs to complete its task. A very
simple example can be different kinds of Views provided by Android OS.
13
public class User {
private static volatile User instance = null;
// other fields / standard constructors / getters
public static User getSingletonInstance(String name, String email, String
country) {
if (instance == null) {
synchronized (User.class) {
if (instance == null) {
instance = new User(name, email, country);
}
}
}
return instance;
}
}
User user = User.getSingletonInstance("John", "john@domain.com", "Argentina");

More Related Content

What's hot (20)

PDF
Java Constructor
Soba Arjun
 
PPT
Java lec constructors
Jan Niño Acierto
 
PPTX
Constructor& destructor
chauhankapil
 
PPTX
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
PDF
Node.js Deserialization
NSConclave
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PDF
Python Deserialization Attacks
NSConclave
 
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPT
Java lec class, objects and constructors
Jan Niño Acierto
 
PPTX
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
PPTX
MVC and Entity Framework
James Johnson
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
Object Oriented JavaScript - II
TO THE NEW | Technology
 
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
paramisoft
 
PPT
Introduction to Design Patterns and Singleton
Jonathan Simon
 
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
PPTX
Constructors
Mirza Hussain
 
PPTX
constructors in java ppt
kunal kishore
 
PPTX
C++Constructors
Nusrat Gulbarga
 
PDF
Object oriented thinking
Minal Maniar
 
Java Constructor
Soba Arjun
 
Java lec constructors
Jan Niño Acierto
 
Constructor& destructor
chauhankapil
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
raja kvk
 
Node.js Deserialization
NSConclave
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
Python Deserialization Attacks
NSConclave
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
Java lec class, objects and constructors
Jan Niño Acierto
 
Singleton Design Pattern - Creation Pattern
Seerat Malik
 
MVC and Entity Framework
James Johnson
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
Object Oriented JavaScript - II
TO THE NEW | Technology
 
Desing pattern prototype-Factory Method, Prototype and Builder
paramisoft
 
Introduction to Design Patterns and Singleton
Jonathan Simon
 
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
Constructors
Mirza Hussain
 
constructors in java ppt
kunal kishore
 
C++Constructors
Nusrat Gulbarga
 
Object oriented thinking
Minal Maniar
 

Similar to Object Oriented Prograring(OOP) java (20)

PPTX
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
PPTX
Object oriented programming in java
Elizabeth alexander
 
PPT
packages and interfaces
madhavi patil
 
PPTX
Ch-2ppt.pptx
ssuser8347a1
 
PPT
Object and Classes in Java
backdoor
 
PPTX
Javasession6
Rajeev Kumar
 
PPT
Java Tutorials
Woxa Technologies
 
PDF
Java Serialization
imypraz
 
PPTX
Gulshan serialization inJava PPT ex.pptx
PRABHATMISHRA969924
 
PDF
Class and Objects in object-oriented programming with Java
Ranjan Karunadipathi
 
PPTX
Creating and destroying objects
Sandeep Chawla
 
PDF
Design Patterns - GOF
Fanus van Straten
 
PPTX
Java basics
Shivanshu Purwar
 
PPTX
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
PPS
Creating classes and applications in java
Gujarat Technological University
 
PPTX
JAVA - Oops Concept.pptx
ayankamila005
 
PPT
java training faridabad
Woxa Technologies
 
PDF
A350103
aijbm
 
PDF
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
szirtmbondo
 
PPTX
More topics on Java
Ahmed Misbah
 
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
Object oriented programming in java
Elizabeth alexander
 
packages and interfaces
madhavi patil
 
Ch-2ppt.pptx
ssuser8347a1
 
Object and Classes in Java
backdoor
 
Javasession6
Rajeev Kumar
 
Java Tutorials
Woxa Technologies
 
Java Serialization
imypraz
 
Gulshan serialization inJava PPT ex.pptx
PRABHATMISHRA969924
 
Class and Objects in object-oriented programming with Java
Ranjan Karunadipathi
 
Creating and destroying objects
Sandeep Chawla
 
Design Patterns - GOF
Fanus van Straten
 
Java basics
Shivanshu Purwar
 
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
Creating classes and applications in java
Gujarat Technological University
 
JAVA - Oops Concept.pptx
ayankamila005
 
java training faridabad
Woxa Technologies
 
A350103
aijbm
 
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
szirtmbondo
 
More topics on Java
Ahmed Misbah
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Ad

Object Oriented Prograring(OOP) java

  • 2. 2 Quetion::explain the five way of creating object in java ● Different ways to create objects in Java As you all know, in Java, a class provides the blueprint for objects, you create an object from a class. There are many different ways to create objects in Java. Following are some ways in which you can create objects in Java:
  • 3. 3 1) Using new Keyword ● 1) Using new Keyword : Using new keyword is the most basic way to create an object. This is the most common way to create an object in java. Almost 99% of objects are created in this way. By using this method we can call any constructor we want to call (no argument or parameterized constructors).
  • 4. 4
  • 5. 5 2) Using New Instance 2) Using New Instance : ● If we know the name of the class & if it has a public default constructor we can create an object –Class.forName. We can use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn’t create any Object. To Create an Object of the Class you have to use the new Instance Method of the Class.
  • 6. 6
  • 7. 7 3) Using clone() method Using clone() method: ● Whenever clone() is called on any object, the JVM actually creates a new object and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor. ● To use clone() method on an object we need to implement Cloneable and define the clone() method in it.
  • 8. 8 . Note : 1)Here we are creating the clone of an existing Object and not any new Object. 2)Class need to implement Cloneable Interface otherwise it will throw CloneNotSupportedException.
  • 9. 9 4) Using deserialization 4) Using deserialization : ● Whenever we serialize and then deserialize an object, JVM creates a separate object. In deserialization, JVM doesn’t use any constructor to create the object. ● To deserialize an object we need to implement the Serializable interface in the class.
  • 11. 11 Deserialization of Object : Object of DeserializationExample class is serialized using writeObject() method and written to file.txt file.
  • 12. 12 5 Factory Method: ● Factory method is a creational design pattern, i.e, related to object creation. In Factory pattern, we create object without exposing the creation logic to client and the client use the same common interface to create new type of object. The idea is to use a static member-function (static factory method) which creates & returns instances, hiding the details of class modules from user. ● A factory pattern is one of the core design principles to create an object, allowing clients to create objects of a library(explained below) in a way such that it doesn’t have tight coupling with the class hierarchy of the library. WHAT IS MEANT WHEN WE TALK ABOUT LIBRARY AND CLIENTS? ● A library is something which is provided by some third party which exposes some public APIs and clients make calls to those public APIs to complete its task. A very simple example can be different kinds of Views provided by Android OS.
  • 13. 13 public class User { private static volatile User instance = null; // other fields / standard constructors / getters public static User getSingletonInstance(String name, String email, String country) { if (instance == null) { synchronized (User.class) { if (instance == null) { instance = new User(name, email, country); } } } return instance; } } User user = User.getSingletonInstance("John", "[email protected]", "Argentina");