SlideShare a Scribd company logo
 OO Programming Concepts
 Creating Objects and Object Reference Variables
› Differences between primitive data type and object type
› Automatic garbage collection
 Constructors
 Modifiers (public, private and static)
 Instance and Class Variables and Methods
 Scope of Variables
 Use the this Keyword
 Case Studies (Mortgage class and Count class)
data field 1
method n
data field n
method 1
An object
...
...
State
Behavior
Data Field
radius = 5
Method
findArea
A Circle object
circle1: Circle
radius = 2
new Circle()
circlen: Circle
radius = 5
new Circle()
...
UML Graphical notation for classes
UML Graphical notation
for objects
Circle
radius: double
findArea(): double
UML Graphical notation for fields
UML Graphical notation for methods
class Circle {
double radius = 1.0;
double findArea(){
return radius * radius * 3.14159;
}
}
ClassName objectReference;
Example:
Circle myCircle;
objectReference = new ClassName();
Example:
myCircle = new Circle();
The object reference is assigned to the object
reference variable.
ClassName objectReference = new ClassName();
Example:
Circle myCircle = new Circle();
1
c: Circle
radius = 1
Primitive type int i = 1 i
Object type Circle c c reference
Created using
new Circle()
1
c1: Circle
radius = 5
Primitive type assignment
i = j
Before:
i
2j
2
After:
i
2j
Object type assignment
c1 = c2
Before:
c1
c2
After:
c1
c2
c2: Circle
radius = 9
As shown in the previous
figure, after the
assignment statement c1 =
c2, c1 points to the same
object referenced by c2.
The object previously
referenced by c1 is no
longer useful. This object
is known as garbage.
Garbage is automatically
TIP: If you know that an
object is no longer
needed, you can explicitly
assign null to a reference
variable for the object.
The Java VM will
automatically collect the
space if the object is not
referenced by any
variable.
 Referencing the object’s data:
objectReference.data
myCircle.radius
 Invoking the object’s method:
objectReference.method
myCircle.findArea()
 Objective: Demonstrate creating
objects, accessing data, and using
methods.
TestCircle Run
Circle(double r) {
radius = r;
}
Circle() {
radius = 1.0;
}
myCircle = new Circle(5.0);
Constructors are a
special kind of
methods that are
invoked to construct
objects.
A constructor with no parameters
is referred to as a default
constructor.
 Constructors must have the
same name as the class itself.
 Constructors do not have a
return type—not even void.
 Constructors are invoked using
the new operator when an object is
created. Constructors play the
 Objective: Demonstrate using classes from
the Java library. Use the JFrame
class in the javax.swing
package to create two frames;
use the methods in the JFrame
class to set the title, size
and location of the frames and
to display the frames.
TestFrame Run
 Objective: Demonstrate the role of
constructors and use them to create
objects.
TestCircleWithConstructors Run
By default, the class, variable, or data can be
accessed by any class in the same package.
 public
The class, data, or method is visible to any class in any
package.
 private
The data or methods can be accessed only by the declaring
class.
The get and set methods are used to read and modify private
properties.
TestCircleWithAccessors Run
In this example, private data are used for the
radius and the accessor methods getRadius and
setRadius are provided for the clients to retrieve
and modify the radius.
 Passing by value (the value is the
reference to the object)
Example 6.5 Passing Objects as Arguments
TestPassingObject Run
main
method
ReferencemyCircle
5n 5
times
printAreas
method
Reference
c
myCircle: Circle
radius = 1
Pass by value (here the value is 5)
Pass by value (here the value is the
reference for the object)
Instance variables belong to a specific instance.
Instance methods are invoked by an instance of
the class.
Class variables are shared by all the instances of the
class.
Class methods are not tied to a specific object.
Class constants are final variables shared by all the
instances of the class.
To declare class variables, constants, and methods,
use the static modifier.
CircleWithStaticVariable
-radius
-numOfObjects
+getRadius(): double
+setRadius(radius: double): void
+getNumOfObjects(): int
+findArea(): double
1 radiuscircle1:Circle
-radius = 1
-numOfObjects = 2
instantiate
instantiate
Memory
2
5 radius
numOfObjects
radius is an instance
variable, and
numOfObjects is a
class variable
UML Notation:
+: public variables or methods
-: private variables or methods
underline: static variables or metods
circle2:Circle
-radius = 5
-numOfObjects = 2
Objective: Demonstrate the roles of
instance and class variables and
their uses. This example adds a
class variable numOfObjects to track
the number of Circle objects
created.
TestCircleWithStaticVariable Run
 The scope of instance and class variables is
the entire class. They can be declared
anywhere inside a class.
 The scope of a local variable starts from its
declaration and continues to the end of the
block that contains the variable. A local
variable must be declared before it can be
used.
 Use this to refer to the current object.
 Use this to invoke other constructors of
the object.
Circle[] circleArray = new
Circle[10];
An array of objects is
actually an array of
reference variables. So
invoking
circleArray[1].findArea()
involves two levels of
referencing as shown in the
next figure. circleArray
Circle[] circleArray = new
Circle[10];
reference Circle object 0circleArray[0]
…
circleArray
circleArray[1]
circleArray[9] Circle object 9
Circle object 1
Example 7: Summarizing the
areas of the circles
TotalArea Run
Class abstraction means to separate class
implementation from the use of the class. The
creator of the class provides a description of
the class and let the user know how the class
can be used. The user of the class does not
need to know how the class is implemented.
The detail of implementation is encapsulated
and hidden from the user.
Mortgage
-annualInterestRate: double
-numOfYears: int
-loanAmount: double
+Mortgage()
+Mortgage(annualInterestRate: double,
numOfYears: int, loanAmount: double)
+getAnnualInterestRate(): double
+getNumOfYears(): int
+getLoanAmount(): double
+setAnnualInterestRate(annualInteresteRate: double): void
+setNumOfYears(numOfYears: int): void
+setLoanAmount(loanAmount: double): void
+monthlyPayment(): double
+totalPayment(): double
TestMortgageClass
Run
Mortgage
RunTestVoteCandidate
 java.lang
Contains core Java classes, such as numeric
classes, strings, and objects. This package is
implicitly imported to every Java program.
 java.awt
Contains classes for graphics.
 java.applet
Contains classes for supporting applets.
 java.io
Contains classes for input and output
streams and files.
 java.util
Contains many utilities, such as date.
 java.net
Contains classes for supporting
network communications.
Java API and Core Java classes,
cont.
 java.awt.image
Contains classes for managing bitmap images.
 java.awt.peer
Platform-specific GUI implementation.
 Others:
java.sql
java.rmi
Java API and Core Java classes,
cont.

More Related Content

What's hot

Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
Padma Kannan
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
Sohanur63
 
Java Methods
Java MethodsJava Methods
Java Methods
Rosmina Joy Cabauatan
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
Java sem i
Java sem iJava sem i
Java sem i
priyankabarhate1
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
Mahmoud Alfarra
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
C++ classes
C++ classesC++ classes
C++ classes
Aayush Patel
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
Abdii Rashid
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
the_wumberlog
 
Core java
Core javaCore java
Core java
Rajkattamuri
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
Majid Saeed
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 

What's hot (20)

Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Java sem i
Java sem iJava sem i
Java sem i
 
Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
C++ classes
C++ classesC++ classes
C++ classes
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
 
Core java
Core javaCore java
Core java
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 

Similar to Object and class in java

06slide
06slide06slide
Module 3 Class and Object.ppt
Module 3 Class and Object.pptModule 3 Class and Object.ppt
Module 3 Class and Object.ppt
RanjithKumar742256
 
Lecture-03 _Java Classes_from FAST-NUCES
Lecture-03 _Java Classes_from FAST-NUCESLecture-03 _Java Classes_from FAST-NUCES
Lecture-03 _Java Classes_from FAST-NUCES
UzairSaeed18
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
Hailsh
 
Class and Object.ppt
Class and Object.pptClass and Object.ppt
Class and Object.ppt
Genta Sahuri
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
sidra tauseef
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 
09_ Objects and Classes in programming.ppt
09_ Objects and Classes in programming.ppt09_ Objects and Classes in programming.ppt
09_ Objects and Classes in programming.ppt
MajedAlAnesi
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Object & classes
Object & classes Object & classes
Object & classes
Paresh Parmar
 
The Ring programming language version 1.7 book - Part 77 of 196
The Ring programming language version 1.7 book - Part 77 of 196The Ring programming language version 1.7 book - Part 77 of 196
The Ring programming language version 1.7 book - Part 77 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189
Mahmoud Samir Fayed
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
NuurAxmed2
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
Morgan309846
 

Similar to Object and class in java (20)

06slide
06slide06slide
06slide
 
Module 3 Class and Object.ppt
Module 3 Class and Object.pptModule 3 Class and Object.ppt
Module 3 Class and Object.ppt
 
Lecture-03 _Java Classes_from FAST-NUCES
Lecture-03 _Java Classes_from FAST-NUCESLecture-03 _Java Classes_from FAST-NUCES
Lecture-03 _Java Classes_from FAST-NUCES
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
Class and Object.ppt
Class and Object.pptClass and Object.ppt
Class and Object.ppt
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
09_ Objects and Classes in programming.ppt
09_ Objects and Classes in programming.ppt09_ Objects and Classes in programming.ppt
09_ Objects and Classes in programming.ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptxIPP-M5-C1-Classes _ Objects python -S2.pptx
IPP-M5-C1-Classes _ Objects python -S2.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object & classes
Object & classes Object & classes
Object & classes
 
The Ring programming language version 1.7 book - Part 77 of 196
The Ring programming language version 1.7 book - Part 77 of 196The Ring programming language version 1.7 book - Part 77 of 196
The Ring programming language version 1.7 book - Part 77 of 196
 
The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
 

More from Umamaheshwariv1

Intro html
Intro htmlIntro html
Intro html
Umamaheshwariv1
 
Create webpages
Create webpagesCreate webpages
Create webpages
Umamaheshwariv1
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
Umamaheshwariv1
 
Introduction to oops
Introduction to oopsIntroduction to oops
Introduction to oops
Umamaheshwariv1
 
Introduction to web design
Introduction to web designIntroduction to web design
Introduction to web design
Umamaheshwariv1
 
Coreldraw
CoreldrawCoreldraw
Coreldraw
Umamaheshwariv1
 
Coreldraw
Coreldraw  Coreldraw
Coreldraw
Umamaheshwariv1
 
Dreamweaver
DreamweaverDreamweaver
Dreamweaver
Umamaheshwariv1
 
The five-generations-of-computer
The five-generations-of-computerThe five-generations-of-computer
The five-generations-of-computer
Umamaheshwariv1
 
Osi model
Osi modelOsi model
Osi model
Umamaheshwariv1
 
Adobe pagemaker
Adobe pagemakerAdobe pagemaker
Adobe pagemaker
Umamaheshwariv1
 
Introduction to dbms
Introduction to dbmsIntroduction to dbms
Introduction to dbms
Umamaheshwariv1
 

More from Umamaheshwariv1 (12)

Intro html
Intro htmlIntro html
Intro html
 
Create webpages
Create webpagesCreate webpages
Create webpages
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
Introduction to oops
Introduction to oopsIntroduction to oops
Introduction to oops
 
Introduction to web design
Introduction to web designIntroduction to web design
Introduction to web design
 
Coreldraw
CoreldrawCoreldraw
Coreldraw
 
Coreldraw
Coreldraw  Coreldraw
Coreldraw
 
Dreamweaver
DreamweaverDreamweaver
Dreamweaver
 
The five-generations-of-computer
The five-generations-of-computerThe five-generations-of-computer
The five-generations-of-computer
 
Osi model
Osi modelOsi model
Osi model
 
Adobe pagemaker
Adobe pagemakerAdobe pagemaker
Adobe pagemaker
 
Introduction to dbms
Introduction to dbmsIntroduction to dbms
Introduction to dbms
 

Recently uploaded

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 

Recently uploaded (20)

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 

Object and class in java

  • 1.
  • 2.  OO Programming Concepts  Creating Objects and Object Reference Variables › Differences between primitive data type and object type › Automatic garbage collection  Constructors  Modifiers (public, private and static)  Instance and Class Variables and Methods  Scope of Variables  Use the this Keyword  Case Studies (Mortgage class and Count class)
  • 3. data field 1 method n data field n method 1 An object ... ... State Behavior Data Field radius = 5 Method findArea A Circle object
  • 4. circle1: Circle radius = 2 new Circle() circlen: Circle radius = 5 new Circle() ... UML Graphical notation for classes UML Graphical notation for objects Circle radius: double findArea(): double UML Graphical notation for fields UML Graphical notation for methods
  • 5. class Circle { double radius = 1.0; double findArea(){ return radius * radius * 3.14159; } }
  • 7. objectReference = new ClassName(); Example: myCircle = new Circle(); The object reference is assigned to the object reference variable.
  • 8. ClassName objectReference = new ClassName(); Example: Circle myCircle = new Circle();
  • 9. 1 c: Circle radius = 1 Primitive type int i = 1 i Object type Circle c c reference Created using new Circle()
  • 10. 1 c1: Circle radius = 5 Primitive type assignment i = j Before: i 2j 2 After: i 2j Object type assignment c1 = c2 Before: c1 c2 After: c1 c2 c2: Circle radius = 9
  • 11. As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer useful. This object is known as garbage. Garbage is automatically
  • 12. TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The Java VM will automatically collect the space if the object is not referenced by any variable.
  • 13.  Referencing the object’s data: objectReference.data myCircle.radius  Invoking the object’s method: objectReference.method myCircle.findArea()
  • 14.  Objective: Demonstrate creating objects, accessing data, and using methods. TestCircle Run
  • 15. Circle(double r) { radius = r; } Circle() { radius = 1.0; } myCircle = new Circle(5.0); Constructors are a special kind of methods that are invoked to construct objects.
  • 16. A constructor with no parameters is referred to as a default constructor.  Constructors must have the same name as the class itself.  Constructors do not have a return type—not even void.  Constructors are invoked using the new operator when an object is created. Constructors play the
  • 17.  Objective: Demonstrate using classes from the Java library. Use the JFrame class in the javax.swing package to create two frames; use the methods in the JFrame class to set the title, size and location of the frames and to display the frames. TestFrame Run
  • 18.  Objective: Demonstrate the role of constructors and use them to create objects. TestCircleWithConstructors Run
  • 19. By default, the class, variable, or data can be accessed by any class in the same package.  public The class, data, or method is visible to any class in any package.  private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties.
  • 20. TestCircleWithAccessors Run In this example, private data are used for the radius and the accessor methods getRadius and setRadius are provided for the clients to retrieve and modify the radius.
  • 21.  Passing by value (the value is the reference to the object) Example 6.5 Passing Objects as Arguments TestPassingObject Run
  • 22. main method ReferencemyCircle 5n 5 times printAreas method Reference c myCircle: Circle radius = 1 Pass by value (here the value is 5) Pass by value (here the value is the reference for the object)
  • 23. Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.
  • 24. Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class.
  • 25. To declare class variables, constants, and methods, use the static modifier.
  • 26. CircleWithStaticVariable -radius -numOfObjects +getRadius(): double +setRadius(radius: double): void +getNumOfObjects(): int +findArea(): double 1 radiuscircle1:Circle -radius = 1 -numOfObjects = 2 instantiate instantiate Memory 2 5 radius numOfObjects radius is an instance variable, and numOfObjects is a class variable UML Notation: +: public variables or methods -: private variables or methods underline: static variables or metods circle2:Circle -radius = 5 -numOfObjects = 2
  • 27. Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created. TestCircleWithStaticVariable Run
  • 28.  The scope of instance and class variables is the entire class. They can be declared anywhere inside a class.  The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.
  • 29.  Use this to refer to the current object.  Use this to invoke other constructors of the object.
  • 30. Circle[] circleArray = new Circle[10]; An array of objects is actually an array of reference variables. So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray
  • 31. Circle[] circleArray = new Circle[10]; reference Circle object 0circleArray[0] … circleArray circleArray[1] circleArray[9] Circle object 9 Circle object 1
  • 32. Example 7: Summarizing the areas of the circles TotalArea Run
  • 33. Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user.
  • 34. Mortgage -annualInterestRate: double -numOfYears: int -loanAmount: double +Mortgage() +Mortgage(annualInterestRate: double, numOfYears: int, loanAmount: double) +getAnnualInterestRate(): double +getNumOfYears(): int +getLoanAmount(): double +setAnnualInterestRate(annualInteresteRate: double): void +setNumOfYears(numOfYears: int): void +setLoanAmount(loanAmount: double): void +monthlyPayment(): double +totalPayment(): double TestMortgageClass Run Mortgage
  • 36.  java.lang Contains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program.  java.awt Contains classes for graphics.  java.applet Contains classes for supporting applets.
  • 37.  java.io Contains classes for input and output streams and files.  java.util Contains many utilities, such as date.  java.net Contains classes for supporting network communications. Java API and Core Java classes, cont.
  • 38.  java.awt.image Contains classes for managing bitmap images.  java.awt.peer Platform-specific GUI implementation.  Others: java.sql java.rmi Java API and Core Java classes, cont.