SlideShare a Scribd company logo
Prof. Priyanka V. Barhate
T.Y.Bsc (computer Science )
 Introduce to classes and objects in Java.
 Understand how some of the OO concepts learnt so far
are supported in Java.
 Understand important features in Java classes.
 Java is a true OO language and therefore the underlying
structure of all Java programs is classes.
 Anything we wish to represent in Java must be encapsulated
in a class that defines the “state” and “behaviour” of the
basic program components known as objects.
 Classes create objects and objects use methods to
communicate between them. They provide a convenient
method for packaging a group of logically related data items
and functions that work on them.
 A class essentially serves as a template for an object and
behaves like a basic data type “int”. It is therefore important
to understand how the fields and methods are defined in a
class and how they are used to build a Java program that
incorporates the basic OO concepts such as encapsulation,
inheritance, and polymorphism.
 A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
Circle
centre
radius
circumference()
area()
 A class is a collection of fields (data) and methods (procedure
or function) that operate on that data.
 The basic syntax for a class definition:
 Bare bone class – no fields, no methods
public class Circle {
// my circle class
}
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}

Add fields

The fields (data) are also called the instance varaibles.
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
 A class with only data fields has no life. Objects
created by such a class cannot respond to any
messages.
 Methods are declared inside the body of the class
but immediately after the declaration of data fields.
 The general form of a method declaration is:
type MethodName (parameter-list)
{
Method-body;
}
public class Circle {
public double x, y; // centre of the circle
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}
}
Method Body
 Declare the Circle class, have created a new data type –
Data Abstraction
 Can define variables (objects) of that type:
Circle aCircle;
Circle bCircle;
 aCircle, bCircle simply refers to a Circle object, not an
object itself.
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null

Objects are created dynamically using the new keyword.

aCircle and bCircle refer to Circle objects
bCircle = new Circle() ;aCircle = new Circle() ;
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
Before Assignment
 The object does not have a reference and cannot
be used in future.
 The object becomes a candidate for automatic garbage
collection.
 Java automatically collects garbage periodically and
releases the memory used to be used in the future.
Q
 Similar to C syntax for accessing data defined in a
structure.
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize center and radius
aCircle.y = 2.0
aCircle.r = 1.0
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
 Using Object Methods:
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
} [raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
Java sem i

More Related Content

What's hot (19)

PPT
Lect 1-class and object
Fajar Baskoro
 
PPT
4 Classes & Objects
Praveen M Jigajinni
 
PDF
ITFT-Classes and object in java
Atul Sehdev
 
PDF
Lect 1-java object-classes
Fajar Baskoro
 
PPT
Class and object in c++
NainaKhan28
 
PPTX
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
PPTX
oop lecture 3
Atif Khan
 
PPT
Class and object in C++
rprajat007
 
PPTX
C++ classes
Aayush Patel
 
PDF
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
PPTX
Chapter2 array of objects
Mahmoud Alfarra
 
PPTX
Classes and objects
rajveer_Pannu
 
PPT
Object & classes
Paresh Parmar
 
PPTX
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
PDF
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
PDF
Java Methods
Rosmina Joy Cabauatan
 
PPT
Lecture 2 classes i
the_wumberlog
 
PPTX
Java class,object,method introduction
Sohanur63
 
Lect 1-class and object
Fajar Baskoro
 
4 Classes & Objects
Praveen M Jigajinni
 
ITFT-Classes and object in java
Atul Sehdev
 
Lect 1-java object-classes
Fajar Baskoro
 
Class and object in c++
NainaKhan28
 
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
oop lecture 3
Atif Khan
 
Class and object in C++
rprajat007
 
C++ classes
Aayush Patel
 
How to write you first class in c++ object oriented programming
Syed Faizan Hassan
 
Chapter2 array of objects
Mahmoud Alfarra
 
Classes and objects
rajveer_Pannu
 
Object & classes
Paresh Parmar
 
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Java Methods
Rosmina Joy Cabauatan
 
Lecture 2 classes i
the_wumberlog
 
Java class,object,method introduction
Sohanur63
 

Similar to Java sem i (20)

PPT
Class and Object.ppt
Genta Sahuri
 
PPT
Unit 1 Part - 2 Class Object.ppt
DeepVala5
 
PPT
Java Presentation.ppt
Morgan309846
 
PPTX
Class and Object.pptx
Hailsh
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPT
Module 3 Class and Object.ppt
RanjithKumar742256
 
PPT
08slide
Dorothea Chaffin
 
PPT
JavaYDL8
Terry Yoast
 
PPT
09slide.ppt
kavitamittal18
 
PPT
09slide.ppt oops classes and objects concept
kavitamittal18
 
PPT
slides 01.ppt
Anwar Ali Yahya
 
PPT
packages and interfaces
madhavi patil
 
PPT
09_ Objects and Classes in programming.ppt
MajedAlAnesi
 
PPT
Chapter 9 Objects and Classes JAVA learning
AhsirYu
 
PDF
Lecture-03 _Java Classes_from FAST-NUCES
UzairSaeed18
 
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
PDF
Introduction to java and oop
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Java
SangeethaSasi1
 
PPTX
Java
SangeethaSasi1
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Class and Object.ppt
Genta Sahuri
 
Unit 1 Part - 2 Class Object.ppt
DeepVala5
 
Java Presentation.ppt
Morgan309846
 
Class and Object.pptx
Hailsh
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Module 3 Class and Object.ppt
RanjithKumar742256
 
JavaYDL8
Terry Yoast
 
09slide.ppt
kavitamittal18
 
09slide.ppt oops classes and objects concept
kavitamittal18
 
slides 01.ppt
Anwar Ali Yahya
 
packages and interfaces
madhavi patil
 
09_ Objects and Classes in programming.ppt
MajedAlAnesi
 
Chapter 9 Objects and Classes JAVA learning
AhsirYu
 
Lecture-03 _Java Classes_from FAST-NUCES
UzairSaeed18
 
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Ad

Recently uploaded (20)

PDF
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
PDF
oil and gas chemical injection system
Okeke Livinus
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
PDF
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
PDF
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
PDF
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
PDF
GUGC Research Overview (December 2024)
Ghent University Global Campus
 
DOCX
Paper - Taboo Language (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
PDF
Portable Hyperspectral Imaging (pHI) for the enhanced recording of archaeolog...
crabbn
 
PDF
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
PDF
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
PDF
Pharmakon of algorithmic alchemy: Marketing in the age of AI
Selcen Ozturkcan
 
PDF
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PPTX
Basal_ganglia_Structure_Function_Importance
muralinath2
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PPTX
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
PDF
Calcium in a supernova remnant as a fingerprint of a sub-Chandrasekhar-mass e...
Sérgio Sacani
 
PDF
Unit-3 ppt.pdf organic chemistry unit 3 heterocyclic
visionshukla007
 
PDF
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
The emergence of galactic thin and thick discs across cosmic history
Sérgio Sacani
 
oil and gas chemical injection system
Okeke Livinus
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
GUGC Research Overview (December 2024)
Ghent University Global Campus
 
Paper - Taboo Language (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
Portable Hyperspectral Imaging (pHI) for the enhanced recording of archaeolog...
crabbn
 
BlackBody Radiation experiment report.pdf
Ghadeer Shaabna
 
Plant growth promoting bacterial non symbiotic
psuvethapalani
 
Pharmakon of algorithmic alchemy: Marketing in the age of AI
Selcen Ozturkcan
 
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Basal_ganglia_Structure_Function_Importance
muralinath2
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
Calcium in a supernova remnant as a fingerprint of a sub-Chandrasekhar-mass e...
Sérgio Sacani
 
Unit-3 ppt.pdf organic chemistry unit 3 heterocyclic
visionshukla007
 
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
Ad

Java sem i

  • 1. Prof. Priyanka V. Barhate T.Y.Bsc (computer Science )
  • 2.  Introduce to classes and objects in Java.  Understand how some of the OO concepts learnt so far are supported in Java.  Understand important features in Java classes.
  • 3.  Java is a true OO language and therefore the underlying structure of all Java programs is classes.  Anything we wish to represent in Java must be encapsulated in a class that defines the “state” and “behaviour” of the basic program components known as objects.  Classes create objects and objects use methods to communicate between them. They provide a convenient method for packaging a group of logically related data items and functions that work on them.  A class essentially serves as a template for an object and behaves like a basic data type “int”. It is therefore important to understand how the fields and methods are defined in a class and how they are used to build a Java program that incorporates the basic OO concepts such as encapsulation, inheritance, and polymorphism.
  • 4.  A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()
  • 5.  A class is a collection of fields (data) and methods (procedure or function) that operate on that data.  The basic syntax for a class definition:  Bare bone class – no fields, no methods public class Circle { // my circle class } class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] }
  • 6.  Add fields  The fields (data) are also called the instance varaibles. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }
  • 7.  A class with only data fields has no life. Objects created by such a class cannot respond to any messages.  Methods are declared inside the body of the class but immediately after the declaration of data fields.  The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }
  • 8. public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } } Method Body
  • 9.  Declare the Circle class, have created a new data type – Data Abstraction  Can define variables (objects) of that type: Circle aCircle; Circle bCircle;
  • 10.  aCircle, bCircle simply refers to a Circle object, not an object itself. aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null null
  • 11.  Objects are created dynamically using the new keyword.  aCircle and bCircle refer to Circle objects bCircle = new Circle() ;aCircle = new Circle() ;
  • 12. Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;
  • 13. Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment
  • 14.  The object does not have a reference and cannot be used in future.  The object becomes a candidate for automatic garbage collection.  Java automatically collects garbage periodically and releases the memory used to be used in the future. Q
  • 15.  Similar to C syntax for accessing data defined in a structure. Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0 ObjectName.VariableName ObjectName.MethodName(parameter-list)
  • 16.  Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle
  • 17. // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String args[]) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.r = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } } [raj@mundroo]%: java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.400000000000002