SlideShare una empresa de Scribd logo
1 de 20
© Prognoz Technologies Pvt. Ltd
Object & Class
© Prognoz Technologies Pvt. Ltd
What is Object ?
 Objects are key to understanding object-oriented technology.
 Look around right now and you'll find many examples of real-world
objects: your dog, your desk, your television set, your bicycle.
2
© Prognoz Technologies Pvt. Ltd
Continue…
 Real-world objects share two characteristics: They all have state and
behavior.
 Dogs have state (name, color, breed, hungry) and behavior (barking,
fetching, wagging tail).
© Prognoz Technologies Pvt. Ltd
Continue…
Bicycles also have state(current gear, current pedal cadence, current speed)
and behavior (changing gear, changing pedal cadence, applying brakes).
4
© Prognoz Technologies Pvt. Ltd
What is Class ?
 In the real world, you'll often find many individual objects all of the same
kind. There may be thousands of other bicycles in existence, all of the
same make and model.
 Each bicycle was built from the same set of blueprints and therefore
contains the same components.
 In object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles.
 A class is the blueprint from which individual objects are created.
© Prognoz Technologies Pvt. Ltd
Continue…
© Prognoz Technologies Pvt. Ltd
Continue…
OBJECTS
CLASSES
© Prognoz Technologies Pvt. Ltd
A Class acts as the template from which an instance of an object is
created. The class defines the properties of the object and the
methods used to control the object's behavior.
A Class specifies the structure of data as well as the methods which
manipulate that data. Such data and methods are contained in each
instance of the class.
A Class is a model or template that can be instantiated to create
objects with a common definition, and therefore common
properties, operations and behavior.
A Class provides a template for defining the behavior of a particular
type of object. Objects are referred to as “instances” of a class.
© Prognoz Technologies Pvt. Ltd
CLASSES
class One
{
}
class Book
{
}
class College
{
}
class AnyThing
{
}
© Prognoz Technologies Pvt. Ltd
Class Definition
A class contains a name, several variable declarations (instance variables)
and several method declarations. All are called members of the class.
General form of a class:
class classname {
type instance-variable-1;
…
type instance-variable-n;
type method-name-1(parameter-list) {
type method-name-2(parameter-list) {
…
type method-name-m(parameter-list) { …
}
© Prognoz Technologies Pvt. Ltd
Example: Class
A class with three variable members:
class Box {
double width;
double height;
double depth;
}
A new Box object is created and a new value assigned to its width variable:
Box myBox = new Box();
myBox.width = 100;
© Prognoz Technologies Pvt. Ltd
Example: Class Usage
class BoxDemo {
public static void main(String args[]
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * m
System.out.println("Volume is " +
}
}
© Prognoz Technologies Pvt. Ltd
Compilation and Execution
Place the Box class definitions in file Box.java:
class Box { … }
Place the BoxDemo class definitions in file BoxDemo.java:
class BoxDemo {
public static void main(…) { … }
}
Compilation and execution:
> javac BoxDemo.java
> java BoxDemo
© Prognoz Technologies Pvt. Ltd
Variable Independence
Each object has its own copy of the instance variables: changing the
variables of one object has no effect on the variables of another object.
© Prognoz Technologies Pvt. Ltd
OBJECTS
class Book
{
}
class JavaBook
{
public ststic void main(String args[])
{
Book b=new Book();
}
}
© Prognoz Technologies Pvt. Ltd
Declaring Objects
Obtaining objects of a class is a two-stage process:
1) Declare a variable of the class type:
Box myBox;
The value of myBox is a reference to an object, if one exists, or null.
At this moment, the value of myBox is null.
2) Acquire an actual, physical copy of an object and assign its address to
the variable. How to do this?
© Prognoz Technologies Pvt. Ltd
Operator new
Allocates memory for a Box object and returns its address:
Box myBox = new Box();
The address is then stored in the myBox reference variable.
Box() is a class constructor - a class may declare its own constructor or
rely on the default constructor provided by the Java environment.
Syntax:
accessing data member of the class: objectname.datamember name;
accessing methods of the class: objectname.method name();
So for accessing data of the class: we have to use (.) dot operator.
© Prognoz Technologies Pvt. Ltd
Memory Allocation
Memory is allocated for objects dynamically.
This has both advantages and disadvantages:
1) as many objects are created as needed
2) allocation is uncertain – memory may be insufficient
Variables of simple types do not require new:
int n = 1;
In the interest of efficiency, Java does not implement simple types as
objects. Variables of simple types hold values, not references.
© Prognoz Technologies Pvt. Ltd
Assigning Reference Variables
Assignment copies address, not the actual value:
Box b1 = new Box();
Box b2 = b1;
Both variables point to the same object.
Variables are not in any way connected. After
b1 = null;
b2 still refers to the original object.
© Prognoz Technologies Pvt. Ltd
Thank you!!
20

Más contenido relacionado

La actualidad más candente (20)

Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Oops
OopsOops
Oops
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Concept of Object Oriented Programming
Concept of Object Oriented Programming Concept of Object Oriented Programming
Concept of Object Oriented Programming
 
Characteristics of oop
Characteristics of oopCharacteristics of oop
Characteristics of oop
 
General oops concepts
General oops conceptsGeneral oops concepts
General oops concepts
 
Ashish oot
Ashish ootAshish oot
Ashish oot
 
Encapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistenceEncapsulation of operations, methods & persistence
Encapsulation of operations, methods & persistence
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
C++ with student management system project
C++ with student management system projectC++ with student management system project
C++ with student management system project
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Oops
OopsOops
Oops
 
Oop concept
Oop conceptOop concept
Oop concept
 
OOP
OOPOOP
OOP
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Oops slide
Oops slide Oops slide
Oops slide
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
encapsulation
encapsulationencapsulation
encapsulation
 

Similar a Basic concept of Object Oriented Programming

JS-02-JavaScript-Objects.ppt
JS-02-JavaScript-Objects.pptJS-02-JavaScript-Objects.ppt
JS-02-JavaScript-Objects.pptMadhukarReddy74
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)dannygriff1
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
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 196Mahmoud Samir Fayed
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfArpitaJana28
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptxShuvrojitMajumder
 

Similar a Basic concept of Object Oriented Programming (20)

Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
JS-02-JavaScript-Objects.ppt
JS-02-JavaScript-Objects.pptJS-02-JavaScript-Objects.ppt
JS-02-JavaScript-Objects.ppt
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
OOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptxOOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptx
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Is2215 lecture2 student(2)
Is2215 lecture2 student(2)Is2215 lecture2 student(2)
Is2215 lecture2 student(2)
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Java basics
Java basicsJava basics
Java basics
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
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
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Cs2305 programming paradigms lecturer notes
Cs2305   programming paradigms lecturer notesCs2305   programming paradigms lecturer notes
Cs2305 programming paradigms lecturer notes
 
Object Oriented Programming.pptx
 Object Oriented Programming.pptx Object Oriented Programming.pptx
Object Oriented Programming.pptx
 

Más de Prognoz Technologies Pvt. Ltd. (9)

Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
A comprehensive software infrastructure of .Net
A comprehensive software infrastructure of .Net  A comprehensive software infrastructure of .Net
A comprehensive software infrastructure of .Net
 
Microsoft C# programming basics
Microsoft C# programming basics  Microsoft C# programming basics
Microsoft C# programming basics
 
Introduction of .net framework
Introduction of .net frameworkIntroduction of .net framework
Introduction of .net framework
 
How to handle exceptions in Java Technology
How to handle exceptions in Java Technology How to handle exceptions in Java Technology
How to handle exceptions in Java Technology
 
Features of java technology
Features of java technologyFeatures of java technology
Features of java technology
 
Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
 
Qualities of a Successful Person
Qualities of a Successful PersonQualities of a Successful Person
Qualities of a Successful Person
 
Quantitative Aptitude Concepts
Quantitative Aptitude ConceptsQuantitative Aptitude Concepts
Quantitative Aptitude Concepts
 

Último

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Último (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

Basic concept of Object Oriented Programming

  • 1. © Prognoz Technologies Pvt. Ltd Object & Class
  • 2. © Prognoz Technologies Pvt. Ltd What is Object ?  Objects are key to understanding object-oriented technology.  Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. 2
  • 3. © Prognoz Technologies Pvt. Ltd Continue…  Real-world objects share two characteristics: They all have state and behavior.  Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
  • 4. © Prognoz Technologies Pvt. Ltd Continue… Bicycles also have state(current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). 4
  • 5. © Prognoz Technologies Pvt. Ltd What is Class ?  In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model.  Each bicycle was built from the same set of blueprints and therefore contains the same components.  In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.  A class is the blueprint from which individual objects are created.
  • 6. © Prognoz Technologies Pvt. Ltd Continue…
  • 7. © Prognoz Technologies Pvt. Ltd Continue… OBJECTS CLASSES
  • 8. © Prognoz Technologies Pvt. Ltd A Class acts as the template from which an instance of an object is created. The class defines the properties of the object and the methods used to control the object's behavior. A Class specifies the structure of data as well as the methods which manipulate that data. Such data and methods are contained in each instance of the class. A Class is a model or template that can be instantiated to create objects with a common definition, and therefore common properties, operations and behavior. A Class provides a template for defining the behavior of a particular type of object. Objects are referred to as “instances” of a class.
  • 9. © Prognoz Technologies Pvt. Ltd CLASSES class One { } class Book { } class College { } class AnyThing { }
  • 10. © Prognoz Technologies Pvt. Ltd Class Definition A class contains a name, several variable declarations (instance variables) and several method declarations. All are called members of the class. General form of a class: class classname { type instance-variable-1; … type instance-variable-n; type method-name-1(parameter-list) { type method-name-2(parameter-list) { … type method-name-m(parameter-list) { … }
  • 11. © Prognoz Technologies Pvt. Ltd Example: Class A class with three variable members: class Box { double width; double height; double depth; } A new Box object is created and a new value assigned to its width variable: Box myBox = new Box(); myBox.width = 100;
  • 12. © Prognoz Technologies Pvt. Ltd Example: Class Usage class BoxDemo { public static void main(String args[] Box mybox = new Box(); double vol; mybox.width = 10; mybox.height = 20; mybox.depth = 15; vol = mybox.width * mybox.height * m System.out.println("Volume is " + } }
  • 13. © Prognoz Technologies Pvt. Ltd Compilation and Execution Place the Box class definitions in file Box.java: class Box { … } Place the BoxDemo class definitions in file BoxDemo.java: class BoxDemo { public static void main(…) { … } } Compilation and execution: > javac BoxDemo.java > java BoxDemo
  • 14. © Prognoz Technologies Pvt. Ltd Variable Independence Each object has its own copy of the instance variables: changing the variables of one object has no effect on the variables of another object.
  • 15. © Prognoz Technologies Pvt. Ltd OBJECTS class Book { } class JavaBook { public ststic void main(String args[]) { Book b=new Book(); } }
  • 16. © Prognoz Technologies Pvt. Ltd Declaring Objects Obtaining objects of a class is a two-stage process: 1) Declare a variable of the class type: Box myBox; The value of myBox is a reference to an object, if one exists, or null. At this moment, the value of myBox is null. 2) Acquire an actual, physical copy of an object and assign its address to the variable. How to do this?
  • 17. © Prognoz Technologies Pvt. Ltd Operator new Allocates memory for a Box object and returns its address: Box myBox = new Box(); The address is then stored in the myBox reference variable. Box() is a class constructor - a class may declare its own constructor or rely on the default constructor provided by the Java environment. Syntax: accessing data member of the class: objectname.datamember name; accessing methods of the class: objectname.method name(); So for accessing data of the class: we have to use (.) dot operator.
  • 18. © Prognoz Technologies Pvt. Ltd Memory Allocation Memory is allocated for objects dynamically. This has both advantages and disadvantages: 1) as many objects are created as needed 2) allocation is uncertain – memory may be insufficient Variables of simple types do not require new: int n = 1; In the interest of efficiency, Java does not implement simple types as objects. Variables of simple types hold values, not references.
  • 19. © Prognoz Technologies Pvt. Ltd Assigning Reference Variables Assignment copies address, not the actual value: Box b1 = new Box(); Box b2 = b1; Both variables point to the same object. Variables are not in any way connected. After b1 = null; b2 still refers to the original object.
  • 20. © Prognoz Technologies Pvt. Ltd Thank you!! 20