SlideShare a Scribd company logo
CLASS
CLASSES
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 template
 It provides a new data type
 It is logical entity , does not exist physically
OBJECT
 The variable of the new data type i.e class is Called object
 It is an instance of the class
 It exists physically i.e take space in memory
 To access the class object is necessary
SYNTAX TO DECLARE CLASS…
 class demo
{
}
 class demo2
{type instance var;
type maethod(parameter lists)
{
}
}
 public class Circle {
double x, y; // centre coordinate
double r; // radius of the circle
}
 The fields (data) are also called the instance varaibles.
 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 {
double x, y; // centre of the circle
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;
}
}
 Declare the Circle class, have created a new data type – Data
Abstraction
 Can define variables (objects) of that type:
Circle aCircle; // Variable of class
Circle bCircle;
aCircle, bCircle simply refers to a Circle object,
not an object itself.
null
11
CREATING OBJECTS OF A CLASS
 Objects are created dynamically using the new keyword.
 aCircle and bCircle refer to Circle objects
bCircle = new Circle() ;aCircle = new Circle() ;
 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
After Assignment
14
ACCESSING OBJECT/CIRCLE DATA
 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)
15
EXECUTING METHODS IN
OBJECT/CIRCLE
 Using Object Methods:
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle

More Related Content

PPT
Java sem i
PPTX
Classes and objects
PPTX
Java Class1
PPTX
Java Class 2
PPTX
Virtualization: Force driving cloud computing
PPTX
Java Datatypes
PPTX
Cloud Computing : Revised Presentation
PPTX
Cloudsim modified
Java sem i
Classes and objects
Java Class1
Java Class 2
Virtualization: Force driving cloud computing
Java Datatypes
Cloud Computing : Revised Presentation
Cloudsim modified

Similar to Java Classes (20)

PDF
Lecture-03 _Java Classes_from FAST-NUCES
PPT
Class and Object.ppt
PPT
Lect 1-class and object
PPT
inheritance of java...basics of java in ppt
PPTX
Class and Object.pptx
PPTX
C++ classes
PPT
Object & classes
PPT
PPT
Lecture 2 classes i
PPTX
Classes and objects
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPTX
Oop objects_classes
PPT
Unit 1 Part - 2 Class Object.ppt
PPT
Module 3 Class and Object.ppt
PPT
Oops concept in c#
PDF
Chapter22 static-class-member-example
PPT
C++ classes tutorials
PPT
C++ classes tutorials
PPTX
C++ classes
Lecture-03 _Java Classes_from FAST-NUCES
Class and Object.ppt
Lect 1-class and object
inheritance of java...basics of java in ppt
Class and Object.pptx
C++ classes
Object & classes
Lecture 2 classes i
Classes and objects
Unit 1 Part - 3 constructor Overloading Static.ppt
Oop objects_classes
Unit 1 Part - 2 Class Object.ppt
Module 3 Class and Object.ppt
Oops concept in c#
Chapter22 static-class-member-example
C++ classes tutorials
C++ classes tutorials
C++ classes
Ad

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Electronic commerce courselecture one. Pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Cloud computing and distributed systems.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Cloud computing and distributed systems.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Ad

Java Classes

  • 2. CLASSES A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()
  • 3.  A Class is a template  It provides a new data type  It is logical entity , does not exist physically
  • 4. OBJECT  The variable of the new data type i.e class is Called object  It is an instance of the class  It exists physically i.e take space in memory  To access the class object is necessary
  • 5. SYNTAX TO DECLARE CLASS…  class demo { }  class demo2 {type instance var; type maethod(parameter lists) { } }
  • 6.  public class Circle { double x, y; // centre coordinate double r; // radius of the circle }  The fields (data) are also called the instance varaibles.
  • 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 { double x, y; // centre of the circle 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; } }
  • 9.  Declare the Circle class, have created a new data type – Data Abstraction  Can define variables (objects) of that type: Circle aCircle; // Variable of class Circle bCircle;
  • 10. aCircle, bCircle simply refers to a Circle object, not an object itself. null
  • 11. 11 CREATING OBJECTS OF A CLASS  Objects are created dynamically using the new keyword.  aCircle and bCircle refer to Circle objects bCircle = new Circle() ;aCircle = new Circle() ;
  • 12.  aCircle = new Circle();  bCircle = new Circle() ;  bCircle = aCircle;
  • 13. 13 CREATING OBJECTS OF A CLASS aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle After Assignment
  • 14. 14 ACCESSING OBJECT/CIRCLE DATA  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)
  • 15. 15 EXECUTING METHODS IN OBJECT/CIRCLE  Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle