SlideShare a Scribd company logo
Nihar Ranjan Roy
WHAT IS A CLASS?
 It is defined as a blueprint or a template from
which an object is declared
[access specifier] [modifiers] class <class name>
{
………………
Members of the class
……………....
}
2
RULES FOR NAMING A CLASS
 A class name should not be a keyword in java
 Name can begin with letter, an _ or $
 Name should not contain embedded space or
period(.)
 It can be of any length
3
WHAT IS AN OBJECT?
 Its an instance of a class.
 Its maintains its states in variables and
implements its behavior with its methods
<Class name><object name>;
Example
Student obj;
Obj=new Student();
Objt=new Student(..arguments);
Or
Student obj=new Student();
4
MEMBERS OF A CLASS
Members
of a class
variables
Instance variables Class variables
Methods
5
HOW TO DECLARE METHODS?
Syntax
[access specifier][modifier]<return_type>
method_name([Parameters])
{
//body
}
Example
int add(int a , int b)
{return (a+b);}
6
HOW TO ACCESS THE MEMBERS?
Obj.<member name>;
ClassName.<member>; for static members
7
NEW OPERATOR?
Declaration Student obj;
obj
Creation obj=new Student()
obj
NULL
address
Name
Roll
8
PROBLEM
Create class named student whose members are
Name// to store student name
Roll //to store his roll no
Init_mem(parameters) //to initialize the members
Disp() // display name and roll
9
10
class Student
{
String name;
int roll;
void set_Value(String n,int r)
{
name=n;
roll=r;
}
void disp()
{
System.out.println("Name is==>"+name);
System.out.println(“Roll is==>"+roll);
}
}
class StudentMClass
{
public static void main(String args[])
{
Student st=new Student();
st.set_Value("Nihar",1);
st.disp();
}
}
HOW TO INITIALIZE AN OBJECT
11
CONSTRUCTORS
1. A constructor is a special function having the
same name that of the class.
2. It has no return type
3. And is invoked through new operator
Syntax
[Modifier]<class_name>([Parameter list])
{
//Body of constructor
}
12
TYPES OF CONSTRUCTORS
Student obj=new Student();
Student obj=new Student( name, roll);
Student obj1=new Student (obj)
13
PROBLEM
Create class named student whose members
are
Name// to store student name
Roll //to store his roll no
Disp() // display name and roll
Implement
Default constructor
Parameter Constructor
Copy Constructors
14
15
class Student
{
String name;
int roll;
Student()
{ System.out.println("===Default===");}
Student(String n,int r)
{
name=n;
roll=r;
System.out.println("===parameter===");
}
Student (Student st)
{name=st.name;
roll=st.roll;
System.out.println("===Copy===");
}
void disp()
{
System.out.println("nName is==>"+name+"nRoll no is==>"+roll);
}
}
Default
Parameter
Copy
16
class StudentCClass
{
public static void main(String args[])
{
Student st=new Student();
st.disp();
Student st1=new Student("Nihar",1);
st1.disp();
Student st2=new Student(st1);
st2.disp();
}
}
Default
Parameter
Copy
class Student
{
String name;
int roll;
/*Student()
{ System.out.println("===Default===");}*/
Student(String n,int r)
{
name=n;
roll=r;
System.out.println("===parameter===");
}
void disp()
{System.out.println("nName is==>"+name+"nRoll no is==>"+roll);
}
}
17
Default
Commented
Parameter
C:myjavaStudentCClass.java:36: cannot find symbol
symbol : constructor Student()
location: class Student
Student st=new Student();
Student st=new Student();
WHAT ARE DESTRUCTORS?
Finalizers are methods that are called immediately
before an object is garbage collected
Syntax
protected void finalize()
{
}
18
KEYWORD “ THIS”
this is used to indicate the current object.
Class Color
{int r,g,b;
Color(int r, int g, int b)
{
r=r, g=g, b=b;
}
}
this.r=r, this.g=g,this.b=b;
19
ACCESS SPECIFIER IN JAVA
20
PUBLIC
public class Student
{
public String name;
public int roll;
public void disp()
{………
……
}
}
Accessible from all other
classes
Inner classes cannot have
public access specifiers
Only one public class can
be declared in a source file
21
PRIVATE
 Only objects of the same class
can have access to private
members
 This provides highest degree of
protection
 Subclasses cannot derived
private members
22
PROTECTED
 Members declared as protected are accessible
from the class itself and its sub class also.
23
UNSPECIFIED/DEFAULT/FRIENDLY
 If non of the access specifiers are specified then it
has package level access i.e all the classes in the
same package can access it.
24
CONCLUSION
25
None
/default
(package)
class
interface
member
Accessible only in its package
Accessible only in its package
Accessible only in its package
private member Accessible only in its class(which
defins it).
protected member Accessible only within its package
and its subclasses
public class
interface
member
Accessible anywhere
Accessible anywhere
Accessible anywhere its class is.
MODIFIERS IN JAVA
Modifiers
static
final
abstract
native
transient
synchronize
volatile
26
27
Modifier Used on Meaning
abstract class
interface
method
Contains unimplemented methods and cannot be instantiated.
All interfaces are abstract. Optional in declarations
No body, only signature. The enclosing class is abstract
final class
method
field
variable
Cannot be subclassed
Cannot be overridden and dynamically looked up
Cannot change its value. static final fields are compile-time
constants.
Cannot change its value.
native method Platform-dependent. No body, only signature
strictfp class
method
All methods in the class are implicitly strictfp.
All floating-point computation done is strictly conforms to
the IEEE 754 standard. All values including intermediate
results must be expressed as IEEE float or double values.
It is rarely used.
MODIFIERS….
MODIFIERS….
28
static class
method
field
initializer
Make an inner class top-level class
A class method, invoked through the class name.
A class field, invoked through the class name
one instance, regardless of class instances created.
Run when the class is loaded, rather than when an instance
is created.
synchronized method For a static method, a lock for the class is acquired before
executing the method. For a non-static method, a lock for
the specific
object instance is acquired.
transient field Not be serialized with the object, used with object
serializations.
volatile field Accessible by unsynchronized threads, very rarely used.
PROBLEM
Write a program in which when ever
an object is created of that class
it prints the no of objects created
till now
29
Ad

More Related Content

Similar to 03_Objects and Classes in java.pdf (20)

Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
Kamlesh Singh
 
Static keyword.pptx
Static keyword.pptxStatic keyword.pptx
Static keyword.pptx
KishanMishra44
 
Java02
Java02Java02
Java02
Vinod siragaon
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
Chap11
Chap11Chap11
Chap11
Terry Yoast
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
Abdii Rashid
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
Jacob William
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
rajkamaltibacademy
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
AshutoshTrivedi30
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
mha4
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
chetanpatilcp783
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
Jacob William
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
shinyduela
 
Best Core Java Training In Bangalore
Best Core Java Training In BangaloreBest Core Java Training In Bangalore
Best Core Java Training In Bangalore
rajkamaltibacademy
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 

More from Parameshwar Maddela (11)

EventHandling in object oriented programming
EventHandling in object oriented programmingEventHandling in object oriented programming
EventHandling in object oriented programming
Parameshwar Maddela
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
working with interfaces in java programming
working with interfaces in java programmingworking with interfaces in java programming
working with interfaces in java programming
Parameshwar Maddela
 
introduction to object orinted programming through java
introduction to object orinted programming through javaintroduction to object orinted programming through java
introduction to object orinted programming through java
Parameshwar Maddela
 
multhi threading concept in oops through java
multhi threading concept in oops through javamulthi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
Object oriented programming -QuestionBank
Object oriented programming -QuestionBankObject oriented programming -QuestionBank
Object oriented programming -QuestionBank
Parameshwar Maddela
 
file handling in object oriented programming through java
file handling in object oriented programming through javafile handling in object oriented programming through java
file handling in object oriented programming through java
Parameshwar Maddela
 
22H51A6755.pptx
22H51A6755.pptx22H51A6755.pptx
22H51A6755.pptx
Parameshwar Maddela
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
Parameshwar Maddela
 
02_Data Types in java.pdf
02_Data Types in java.pdf02_Data Types in java.pdf
02_Data Types in java.pdf
Parameshwar Maddela
 
Intro tooop
Intro tooopIntro tooop
Intro tooop
Parameshwar Maddela
 
EventHandling in object oriented programming
EventHandling in object oriented programmingEventHandling in object oriented programming
EventHandling in object oriented programming
Parameshwar Maddela
 
Exception‐Handling in object oriented programming
Exception‐Handling in object oriented programmingException‐Handling in object oriented programming
Exception‐Handling in object oriented programming
Parameshwar Maddela
 
working with interfaces in java programming
working with interfaces in java programmingworking with interfaces in java programming
working with interfaces in java programming
Parameshwar Maddela
 
introduction to object orinted programming through java
introduction to object orinted programming through javaintroduction to object orinted programming through java
introduction to object orinted programming through java
Parameshwar Maddela
 
multhi threading concept in oops through java
multhi threading concept in oops through javamulthi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
Object oriented programming -QuestionBank
Object oriented programming -QuestionBankObject oriented programming -QuestionBank
Object oriented programming -QuestionBank
Parameshwar Maddela
 
file handling in object oriented programming through java
file handling in object oriented programming through javafile handling in object oriented programming through java
file handling in object oriented programming through java
Parameshwar Maddela
 
Ad

Recently uploaded (20)

Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
PoojaSen20
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
DEATH & ITS TYPES AND PHYSIOLOGICAL CHANGES IN BODY AFTER DEATH, PATIENT WILL...
PoojaSen20
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Module_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptxModule_2_Types_and_Approaches_of_Research (2).pptx
Module_2_Types_and_Approaches_of_Research (2).pptx
drroxannekemp
 
Ad

03_Objects and Classes in java.pdf

  • 2. WHAT IS A CLASS?  It is defined as a blueprint or a template from which an object is declared [access specifier] [modifiers] class <class name> { ……………… Members of the class …………….... } 2
  • 3. RULES FOR NAMING A CLASS  A class name should not be a keyword in java  Name can begin with letter, an _ or $  Name should not contain embedded space or period(.)  It can be of any length 3
  • 4. WHAT IS AN OBJECT?  Its an instance of a class.  Its maintains its states in variables and implements its behavior with its methods <Class name><object name>; Example Student obj; Obj=new Student(); Objt=new Student(..arguments); Or Student obj=new Student(); 4
  • 5. MEMBERS OF A CLASS Members of a class variables Instance variables Class variables Methods 5
  • 6. HOW TO DECLARE METHODS? Syntax [access specifier][modifier]<return_type> method_name([Parameters]) { //body } Example int add(int a , int b) {return (a+b);} 6
  • 7. HOW TO ACCESS THE MEMBERS? Obj.<member name>; ClassName.<member>; for static members 7
  • 8. NEW OPERATOR? Declaration Student obj; obj Creation obj=new Student() obj NULL address Name Roll 8
  • 9. PROBLEM Create class named student whose members are Name// to store student name Roll //to store his roll no Init_mem(parameters) //to initialize the members Disp() // display name and roll 9
  • 10. 10 class Student { String name; int roll; void set_Value(String n,int r) { name=n; roll=r; } void disp() { System.out.println("Name is==>"+name); System.out.println(“Roll is==>"+roll); } } class StudentMClass { public static void main(String args[]) { Student st=new Student(); st.set_Value("Nihar",1); st.disp(); } }
  • 11. HOW TO INITIALIZE AN OBJECT 11
  • 12. CONSTRUCTORS 1. A constructor is a special function having the same name that of the class. 2. It has no return type 3. And is invoked through new operator Syntax [Modifier]<class_name>([Parameter list]) { //Body of constructor } 12
  • 13. TYPES OF CONSTRUCTORS Student obj=new Student(); Student obj=new Student( name, roll); Student obj1=new Student (obj) 13
  • 14. PROBLEM Create class named student whose members are Name// to store student name Roll //to store his roll no Disp() // display name and roll Implement Default constructor Parameter Constructor Copy Constructors 14
  • 15. 15 class Student { String name; int roll; Student() { System.out.println("===Default===");} Student(String n,int r) { name=n; roll=r; System.out.println("===parameter==="); } Student (Student st) {name=st.name; roll=st.roll; System.out.println("===Copy==="); } void disp() { System.out.println("nName is==>"+name+"nRoll no is==>"+roll); } } Default Parameter Copy
  • 16. 16 class StudentCClass { public static void main(String args[]) { Student st=new Student(); st.disp(); Student st1=new Student("Nihar",1); st1.disp(); Student st2=new Student(st1); st2.disp(); } } Default Parameter Copy
  • 17. class Student { String name; int roll; /*Student() { System.out.println("===Default===");}*/ Student(String n,int r) { name=n; roll=r; System.out.println("===parameter==="); } void disp() {System.out.println("nName is==>"+name+"nRoll no is==>"+roll); } } 17 Default Commented Parameter C:myjavaStudentCClass.java:36: cannot find symbol symbol : constructor Student() location: class Student Student st=new Student(); Student st=new Student();
  • 18. WHAT ARE DESTRUCTORS? Finalizers are methods that are called immediately before an object is garbage collected Syntax protected void finalize() { } 18
  • 19. KEYWORD “ THIS” this is used to indicate the current object. Class Color {int r,g,b; Color(int r, int g, int b) { r=r, g=g, b=b; } } this.r=r, this.g=g,this.b=b; 19
  • 21. PUBLIC public class Student { public String name; public int roll; public void disp() {……… …… } } Accessible from all other classes Inner classes cannot have public access specifiers Only one public class can be declared in a source file 21
  • 22. PRIVATE  Only objects of the same class can have access to private members  This provides highest degree of protection  Subclasses cannot derived private members 22
  • 23. PROTECTED  Members declared as protected are accessible from the class itself and its sub class also. 23
  • 24. UNSPECIFIED/DEFAULT/FRIENDLY  If non of the access specifiers are specified then it has package level access i.e all the classes in the same package can access it. 24
  • 25. CONCLUSION 25 None /default (package) class interface member Accessible only in its package Accessible only in its package Accessible only in its package private member Accessible only in its class(which defins it). protected member Accessible only within its package and its subclasses public class interface member Accessible anywhere Accessible anywhere Accessible anywhere its class is.
  • 27. 27 Modifier Used on Meaning abstract class interface method Contains unimplemented methods and cannot be instantiated. All interfaces are abstract. Optional in declarations No body, only signature. The enclosing class is abstract final class method field variable Cannot be subclassed Cannot be overridden and dynamically looked up Cannot change its value. static final fields are compile-time constants. Cannot change its value. native method Platform-dependent. No body, only signature strictfp class method All methods in the class are implicitly strictfp. All floating-point computation done is strictly conforms to the IEEE 754 standard. All values including intermediate results must be expressed as IEEE float or double values. It is rarely used. MODIFIERS….
  • 28. MODIFIERS…. 28 static class method field initializer Make an inner class top-level class A class method, invoked through the class name. A class field, invoked through the class name one instance, regardless of class instances created. Run when the class is loaded, rather than when an instance is created. synchronized method For a static method, a lock for the class is acquired before executing the method. For a non-static method, a lock for the specific object instance is acquired. transient field Not be serialized with the object, used with object serializations. volatile field Accessible by unsynchronized threads, very rarely used.
  • 29. PROBLEM Write a program in which when ever an object is created of that class it prints the no of objects created till now 29
  翻译: