SlideShare a Scribd company logo
Classes & Objects
Class & Object
• A Class is a description of a group of objects with common properties (attributes) & behavior
(operations)
• It serves like a blue print or prototype or template for creating objects
• An Object is an instance of a class.
• All objects have state and behaviour which is defined by its class
Marry
Jane Steffy
Student
Object Characteristics - Identity
• Every object has an unique identity. Two objects may have same state, but can not have same
identity.
• Even if states of two objects are same, their identities should be different. (Student Number. may
work as identity)
Marry is listening class Jane is listening class
Concept of Class
An object is an instance of a class
e.g. Mary is an object of Student class
Jane is an object of Student class
Mary 1001
name rollNo
Jane 1002
name rollNo
Example of class
class Student {
String name;
int rollNumber;
char gender;
void writing (){ }
void listening (){ }
}
• Here class is a keyword and Student is the name of the class having
attributes name, roll Number and gender and a method listening().
Data Members
(State)
Method
(Behavior)
Object
• An object is an instance of a class
• The new operator creates a object & returns a reference to it
• Memory allocation of objects happens in the heap area
• Reference returned can be stored in reference variables
Student s;
s = new Student();
or
Student s = new Student();
s is a reference
variable
new keyword creates
an object and
returns a reference
to it
Instance Members
• A variable declared within a class and outside the
method is an instance variable.
• Non static method is called an instance method.
• The runtime system creates a copy of each instance
variable for every object created for that class.
• An instance member is initialized to its default value.
public class Student{
String name; //instance variables
int rollNumber;
public void listening(){ } //instance method
}
Instance Members
Memory Heap
jane
public class Student{
String name;
int rollNumber;
char gender;
public void listening(){
}
}
Student mary = new Student();
Student jane = new Student();
name
rollNumber
gender
name
rollNumber
gender
mary
Object
Object
References
mary.listening();
Java Naming Conventions
public class Student{
static final String ADDRESS = “PINNACLE, 1310 S. Stemmons Freeway”;
private String name;
private int rollNumber;
public String getName(){
return name;
}
public void setName(String name){
this.name=name; }
public void listeningProgramme(){
// ...
}
}
Class names are nouns in
upper camel case.
Constants should
be in all
uppercase letters
Variable names in
lower camel case.
Methods should be verbs,
in lower camel case.
Class (or static) Members
• A class member is declared by using the static modifier
• The static members belong to the class not to a specific object and hence a
single copy is shared by all objects
• static keyword can be used in three scenarios:
• For class variables
• For methods
• For a block of code
Using static
• static variable
• Belongs to a class
• A single copy to be shared by all instances of the class
• Creation of instance not necessary for using static variables
• Accessed using <class-name>.<variable-name> unlike instance variables which are accessed
as <object-name>.<variable-name>
• static method
• It is a class method
• Accessed using <class name>.<method name>
• Creation of instance not necessary for using static methods
• A static method can access only other static data & methods, and not non-static members
Using static
• static block: A block of statement inside a Java class that is executed
only once when a class is loaded
• Used to initialize static variables of the class
• Static blocks cannot return. They are simply nameless blocks of code that
execute in the order they are specified.
class Test{
static{
//Code goes here
}
}
Static and Instance Members
• Static methods can access static variables and static methods.
• Instance methods can access static variables and static methods.
• Static methods cannot directly access instance variables or instance
methods.
• Both instance and class variable is assigned default value
this
• this can be used inside any method to refer to the current object.
• When a local variable has the same name as an instance variable, the
local variable hides the instance variable.

More Related Content

Similar to 3.Classes&Objects.pptx

Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
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
 
OOP concepts
OOP conceptsOOP concepts
OOP concepts
Azaz Maverick
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
hemanth248901
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 
Java Lecture5.pptx
Java Lecture5.pptxJava Lecture5.pptx
Java Lecture5.pptx
ranjanarora11
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
teach4uin
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
Ravi_Kant_Sahu
 
Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
Nilesh Dalvi
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
ayaankim007
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
SakhilejasonMsibi
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 

Similar to 3.Classes&Objects.pptx (20)

Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
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
 
OOP concepts
OOP conceptsOOP concepts
OOP concepts
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
Java Lecture5.pptx
Java Lecture5.pptxJava Lecture5.pptx
Java Lecture5.pptx
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Explain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).pptExplain Classes and methods in java (ch04).ppt
Explain Classes and methods in java (ch04).ppt
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Lecture 4 part 2.pdf
Lecture 4 part 2.pdfLecture 4 part 2.pdf
Lecture 4 part 2.pdf
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 

Recently uploaded

A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
Bruce Bennett
 
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
dsnow9802
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
cahgading001
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
taqyea
 
Tape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdfTape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdf
KateRobinson68
 
Lbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdfLbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdf
ashiquepa3
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
Bruce Bennett
 
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
2zjra9bn
 
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employeesLeave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Sreenivas702647
 
thyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatialthyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatial
Aditya Raghav
 
Leadership Ambassador club Adventist module
Leadership Ambassador club Adventist moduleLeadership Ambassador club Adventist module
Leadership Ambassador club Adventist module
kakomaeric00
 
Introducing Gopay Mobile App For Environment.pptx
Introducing Gopay Mobile App For Environment.pptxIntroducing Gopay Mobile App For Environment.pptx
Introducing Gopay Mobile App For Environment.pptx
FauzanHarits1
 
0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf
Thomas GIRARD BDes
 
IT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a RoadmapIT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a Roadmap
Base Camp
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
SocMediaFin - Joyce Sullivan
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
Ghh
 
5 Common Mistakes to Avoid During the Job Application Process.pdf
5 Common Mistakes to Avoid During the Job Application Process.pdf5 Common Mistakes to Avoid During the Job Application Process.pdf
5 Common Mistakes to Avoid During the Job Application Process.pdf
Alliance Jobs
 
Job Finding Apps Everything You Need to Know in 2024
Job Finding Apps Everything You Need to Know in 2024Job Finding Apps Everything You Need to Know in 2024
Job Finding Apps Everything You Need to Know in 2024
SnapJob
 
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
NWEXAM
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
GabrielleSinaga
 

Recently uploaded (20)

A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024A Guide to a Winning Interview June 2024
A Guide to a Winning Interview June 2024
 
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...
 
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAANBUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
BUKU PENJAGAAN BUKU PENJAGAAN BUKU PENJAGAAN
 
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
一比一原版布拉德福德大学毕业证(bradford毕业证)如何办理
 
Tape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdfTape Measure Training & Practice Assessments.pdf
Tape Measure Training & Practice Assessments.pdf
 
Lbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdfLbs last rank 2023 9988kr47h4744j445.pdf
Lbs last rank 2023 9988kr47h4744j445.pdf
 
Learnings from Successful Jobs Searchers
Learnings from Successful Jobs SearchersLearnings from Successful Jobs Searchers
Learnings from Successful Jobs Searchers
 
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
官方认证美国旧金山州立大学毕业证学位证书案例原版一模一样
 
Leave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employeesLeave-rules.ppt CCS leave rules 1972 for central govt employees
Leave-rules.ppt CCS leave rules 1972 for central govt employees
 
thyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatialthyroid case presentation.pptx Kamala's Lakshaman palatial
thyroid case presentation.pptx Kamala's Lakshaman palatial
 
Leadership Ambassador club Adventist module
Leadership Ambassador club Adventist moduleLeadership Ambassador club Adventist module
Leadership Ambassador club Adventist module
 
Introducing Gopay Mobile App For Environment.pptx
Introducing Gopay Mobile App For Environment.pptxIntroducing Gopay Mobile App For Environment.pptx
Introducing Gopay Mobile App For Environment.pptx
 
0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf0624.speakingengagementsandteaching-01.pdf
0624.speakingengagementsandteaching-01.pdf
 
IT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a RoadmapIT Career Hacks Navigate the Tech Jungle with a Roadmap
IT Career Hacks Navigate the Tech Jungle with a Roadmap
 
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdfSwitching Careers Slides - JoyceMSullivan SocMediaFin -  2024Jun11.pdf
Switching Careers Slides - JoyceMSullivan SocMediaFin - 2024Jun11.pdf
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
 
5 Common Mistakes to Avoid During the Job Application Process.pdf
5 Common Mistakes to Avoid During the Job Application Process.pdf5 Common Mistakes to Avoid During the Job Application Process.pdf
5 Common Mistakes to Avoid During the Job Application Process.pdf
 
Job Finding Apps Everything You Need to Know in 2024
Job Finding Apps Everything You Need to Know in 2024Job Finding Apps Everything You Need to Know in 2024
Job Finding Apps Everything You Need to Know in 2024
 
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?
 
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
Gabrielle M. A. Sinaga Portfolio, Film Student (2024)
 

3.Classes&Objects.pptx

  • 2. Class & Object • A Class is a description of a group of objects with common properties (attributes) & behavior (operations) • It serves like a blue print or prototype or template for creating objects • An Object is an instance of a class. • All objects have state and behaviour which is defined by its class Marry Jane Steffy Student
  • 3. Object Characteristics - Identity • Every object has an unique identity. Two objects may have same state, but can not have same identity. • Even if states of two objects are same, their identities should be different. (Student Number. may work as identity) Marry is listening class Jane is listening class
  • 4. Concept of Class An object is an instance of a class e.g. Mary is an object of Student class Jane is an object of Student class Mary 1001 name rollNo Jane 1002 name rollNo
  • 5. Example of class class Student { String name; int rollNumber; char gender; void writing (){ } void listening (){ } } • Here class is a keyword and Student is the name of the class having attributes name, roll Number and gender and a method listening(). Data Members (State) Method (Behavior)
  • 6. Object • An object is an instance of a class • The new operator creates a object & returns a reference to it • Memory allocation of objects happens in the heap area • Reference returned can be stored in reference variables Student s; s = new Student(); or Student s = new Student(); s is a reference variable new keyword creates an object and returns a reference to it
  • 7. Instance Members • A variable declared within a class and outside the method is an instance variable. • Non static method is called an instance method. • The runtime system creates a copy of each instance variable for every object created for that class. • An instance member is initialized to its default value. public class Student{ String name; //instance variables int rollNumber; public void listening(){ } //instance method }
  • 8. Instance Members Memory Heap jane public class Student{ String name; int rollNumber; char gender; public void listening(){ } } Student mary = new Student(); Student jane = new Student(); name rollNumber gender name rollNumber gender mary Object Object References mary.listening();
  • 9. Java Naming Conventions public class Student{ static final String ADDRESS = “PINNACLE, 1310 S. Stemmons Freeway”; private String name; private int rollNumber; public String getName(){ return name; } public void setName(String name){ this.name=name; } public void listeningProgramme(){ // ... } } Class names are nouns in upper camel case. Constants should be in all uppercase letters Variable names in lower camel case. Methods should be verbs, in lower camel case.
  • 10. Class (or static) Members • A class member is declared by using the static modifier • The static members belong to the class not to a specific object and hence a single copy is shared by all objects • static keyword can be used in three scenarios: • For class variables • For methods • For a block of code
  • 11. Using static • static variable • Belongs to a class • A single copy to be shared by all instances of the class • Creation of instance not necessary for using static variables • Accessed using <class-name>.<variable-name> unlike instance variables which are accessed as <object-name>.<variable-name> • static method • It is a class method • Accessed using <class name>.<method name> • Creation of instance not necessary for using static methods • A static method can access only other static data & methods, and not non-static members
  • 12. Using static • static block: A block of statement inside a Java class that is executed only once when a class is loaded • Used to initialize static variables of the class • Static blocks cannot return. They are simply nameless blocks of code that execute in the order they are specified. class Test{ static{ //Code goes here } }
  • 13. Static and Instance Members • Static methods can access static variables and static methods. • Instance methods can access static variables and static methods. • Static methods cannot directly access instance variables or instance methods. • Both instance and class variable is assigned default value
  • 14. this • this can be used inside any method to refer to the current object. • When a local variable has the same name as an instance variable, the local variable hides the instance variable.