尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
JAVA
Class
Prepared by
Miss. Arati A. Gadgil
2
3
Class
class is the template for an object and that a class is a way to
encapsulate both data and the functions that operate on that
data.
We define a class by using the class keyword along with the
class name,
class MyClass
{
}
4
Object
A class consists of a collection of fields, or variables, very
much like the named fields of a struct ,all the operations
(called methods) that can be performed on those fields.
A class describes objects and operations defined on those
objects.
To create an object from a class, we type the class's name
followed by the name of the object. For example, the line
below creates an object from the MyClass class,
MyClass myObject = new MyClass();
5
Declaring Fields for a Class
We declare fields for class in much the same way as any
variable in a program, by typing the data type of the field
followed by the name of the field, like this:
int name;
Class fields that is data field is by default accessible only by
methods in the same package.
we can change the rules of this access by using the public,
protected, and private keywords.
6
Public
A public data field can be accessed by any part of a program,
inside or outside of the class in which it's defined.
Protected
A protected data field can only be accessed from within the
class or from within a derivedclass (a subclass).
Private
A private data field cannot even be accessed by a derived
class.
7
Defining Methods
Method describe behavior of an object. A method is a
collection of statements that are group together to perform an
operation.
Syntax :
return-type methodName(parameter-list)
{
//body of method
}
8
void show()
{
System.out.println(“Welcome…”);
}
Here return type is void ,show is name.
int getNo()
{
no=10;
return no;
}
Here return type is int ,getNo is name.
9
Parameter is variable defined by a method that receives
value when the method is called. Parameter are always
local to the method they dont have scope outside the
method. While argument is a value that is passed to a
method when it is called.
10
There are two ways to pass an argument to a method
call-by-value : In this approach copy of an argument value is
pass to a method. Changes made to the argument value inside
the method will have no effect on the arguments.
call-by-reference : In this reference of an argument is pass to
a method. Any changes made inside the method will affect the
agrument value.
11
call-by-value
public class A
{
int a;
public void callByValue(int x)
{
a=x;
System.out.println(a);
}
public static void main(String[] args)
{
A obj = new A(10);
obj.callByValue(x); //function call
}
}
12
call-by-reference
public class A
{ int x=10; int y=20;
public void callByReference(A t)
{
t.x=100; t.y=50;
}
public static void main(String[] args)
{
A s = new A();
System.out.println("Before "+s.x+" "+s.y);
s.callByReference(s);
System.out.println("After "+s.x+" "+s.y);
}
}
13
Controlling Access to Members of a Class
Access level modifiers determine whether other classes can
use a particular field or invoke a particular method. There are
two levels of access control:
1.At the top level—public, or package-private (no explicit
modifier).
2. At the member level—public, private, protected,
or package-private (no explicit modifier).
14
A class may be declared with the modifier public, in which
case that class is visible to all classes everywhere. If a class
has no modifier, it is visible only within its own package.
The following table shows the access to members permitted by
each modifier.
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
15
Constructor
Constructor is special type of method, enables an object to
initialize itself when it's created. A constructor is a public
method with the same name as the class.
class A
{
int a;
public A(int value)
{
a = value;
}
}
16
Class's constructor starts with the public keyword. This is
important because we want to be able to create an object from
the class anywhere in program, and when we create an
object, you're actually calling its constructor.
After the public keyword comes the name of the
constructor followed by the constructor's arguments in
parentheses. When you create an object of the
class, you must also provide the required arguments.
17
There are two types of constructor
Default Constructor
Parameterized constructor
Each time a new object is created at least one constructor will
be invoked.
A obj1=new A()//Default constructor invoked
A obj2=new A() //Parameterized constructor invoked
18
Constructor Overloading
Overloaded constructors are differentiated on the basis of their
type of parameters or number of parameters.
class A
{
int a;
public A()
{
a=0;
}
public A(int value)
{
a = value;
}
}
19
Using the this Keyword
Within an instance method or a constructor, this is a reference
to the current object — the object whose method or constructor
is being called.
we can refer to any member of the current object from within
an instance method or a constructor by using this.
20
public class Point
{ public int x = 0;
public int y = 0;
public Point(int x, int y)
{ this.x = x;
this.y = y; }
}
Each argument to the constructor shadows one of the object's
fields — inside the constructor x is a local copy of the
constructor's first argument. To refer to the Point field x, the
constructor must use this.x.
21
Static
Same data is used for all the instances (objects) of some Class.
Class A
{
public int y = 0;
public static int x= 1;
};
A a = new A();
A b = new A();
System.out.println(b.x);
a.x = 5;
System.out.println(b.x);
A.x= 10;
System.out.println(b.x);
Assignment performed
on the first access to the
Class.
Only one instance of ‘x’
exists in memory
Output:
1
5
10
a b
y y
A.x_
0 0
1
22
Static member function
Static member function can access only static members
Static member function can be called without an instance.
Class TeaPot
{ private static int numOfTP = 0;
private Color myColor_;
public TeaPot(Color c)
{ myColor_ = c;
numOfTP++; }
public static int howManyTeaPots()
{ return numOfTP; }
// error : public static Color getColor()
{ return myColor_; }
}
23
TeaPot tp1 = new TeaPot(Color.RED);
TeaPot tp2 = new TeaPot(Color.GREEN);
System.out.println(“We have “ +
TeaPot.howManyTeaPots()+ “Tea Pots”);
Static Block
Code that is executed in the first reference to the class.
Several static blocks can exist in the same class
( Execution order is by the appearance order in the class
definition ).
Only static members can be accessed.
24
Class design hints
1.Always keep data private.
2.Always initialize data.
3.Don’t use too many basic type in class.
4.Not all fields indivisual field accessor and mutators.
5.Brak up classes that have too many responsibilities.
6.Make the name of your classes and method that reflect their
responsibilty.
Thank You
25

More Related Content

What's hot

Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
SURBHI SAROHA
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
Mohamed Krar
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
manish kumar
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
DevaKumari Vijay
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
Mahmoud Ali
 
Generics
GenericsGenerics
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Generics in java
Generics in javaGenerics in java
Generics in java
suraj pandey
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)
SURBHI SAROHA
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
lykado0dles
 
Generics collections
Generics collectionsGenerics collections
Generics collections
Yaswanth Babu Gummadivelli
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
Eduardo Bergavera
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 

What's hot (20)

Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Generics
GenericsGenerics
Generics
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 

Viewers also liked

Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
Arati Gadgil
 
Shot list
Shot listShot list
Shot list
Omar Hassan
 
Vines presentation
Vines presentationVines presentation
Vines presentation
Omar Hassan
 
Story board
Story boardStory board
Story board
Omar Hassan
 
Horror (DISTINCT)
Horror (DISTINCT)Horror (DISTINCT)
Horror (DISTINCT)
Omar Hassan
 
Recce
RecceRecce
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
Venkat Bathem
 
Java networking
Java networkingJava networking
Java networking
Arati Gadgil
 
Java basic
Java basicJava basic
Java basic
Arati Gadgil
 
Props and budget list
Props and budget listProps and budget list
Props and budget list
Omar Hassan
 
Script and production schedule
Script and production scheduleScript and production schedule
Script and production schedule
Omar Hassan
 
Java stream
Java streamJava stream
Java stream
Arati Gadgil
 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Java package
Java packageJava package
Java package
Arati Gadgil
 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
 

Viewers also liked (17)

Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
 
Shot list
Shot listShot list
Shot list
 
Vines presentation
Vines presentationVines presentation
Vines presentation
 
Story board
Story boardStory board
Story board
 
Horror (DISTINCT)
Horror (DISTINCT)Horror (DISTINCT)
Horror (DISTINCT)
 
Recce
RecceRecce
Recce
 
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
Resume_Informatica_4.3yrs_CSC_MCA_from_NIT_Venkat_CV.v1.0
 
Java networking
Java networkingJava networking
Java networking
 
Java basic
Java basicJava basic
Java basic
 
Props and budget list
Props and budget listProps and budget list
Props and budget list
 
Script and production schedule
Script and production scheduleScript and production schedule
Script and production schedule
 
Java stream
Java streamJava stream
Java stream
 
Java thread
Java threadJava thread
Java thread
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java package
Java packageJava package
Java package
 
Java awt
Java awtJava awt
Java awt
 

Similar to Java class

CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
Mahmoud Ouf
 
Hemajava
HemajavaHemajava
Hemajava
SangeethaSasi1
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
trixiacruz
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
mohamedsamyali
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
Eduardo Bergavera
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
elliando dias
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
DeepVala5
 
Java reflection
Java reflectionJava reflection
Java reflection
Ranjith Chaz
 
Object Oriented Programming - 5. Class & Object
Object Oriented Programming - 5. Class & ObjectObject Oriented Programming - 5. Class & Object
Object Oriented Programming - 5. Class & Object
AndiNurkholis1
 
Inheritance
InheritanceInheritance
Inheritance
Mavoori Soshmitha
 

Similar to Java class (20)

CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Intake 37 4
Intake 37 4Intake 37 4
Intake 37 4
 
Hemajava
HemajavaHemajava
Hemajava
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Object and class
Object and classObject and class
Object and class
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
Java reflection
Java reflectionJava reflection
Java reflection
 
Object Oriented Programming - 5. Class & Object
Object Oriented Programming - 5. Class & ObjectObject Oriented Programming - 5. Class & Object
Object Oriented Programming - 5. Class & Object
 
Inheritance
InheritanceInheritance
Inheritance
 

More from Arati Gadgil

Java adapter
Java adapterJava adapter
Java adapter
Arati Gadgil
 
Java swing
Java swingJava swing
Java swing
Arati Gadgil
 
Java applet
Java appletJava applet
Java applet
Arati Gadgil
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
Arati Gadgil
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Java collection
Java collectionJava collection
Java collection
Arati Gadgil
 

More from Arati Gadgil (7)

Java adapter
Java adapterJava adapter
Java adapter
 
Java swing
Java swingJava swing
Java swing
 
Java applet
Java appletJava applet
Java applet
 
Java layoutmanager
Java layoutmanagerJava layoutmanager
Java layoutmanager
 
Java interface
Java interfaceJava interface
Java interface
 
Java exception
Java exception Java exception
Java exception
 
Java collection
Java collectionJava collection
Java collection
 

Recently uploaded

Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
whatchangedhowreflec
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
Quizzito The Quiz Society of Gargi College
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
chaudharyreet2244
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
heathfieldcps1
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
MattVassar1
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
Ben Aldrich
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
Kalna College
 
The Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptxThe Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptx
PriyaKumari928991
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
Frederic Fovet
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
Kalna College
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
shabeluno
 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
Celine George
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Celine George
 

Recently uploaded (20)

Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
 
A Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by QuizzitoA Quiz on Drug Abuse Awareness by Quizzito
A Quiz on Drug Abuse Awareness by Quizzito
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
 
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx78 Microsoft-Publisher - Sirin Sultana Bora.pptx
78 Microsoft-Publisher - Sirin Sultana Bora.pptx
 
The Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptxThe Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptx
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
 

Java class

  • 2. 2
  • 3. 3 Class class is the template for an object and that a class is a way to encapsulate both data and the functions that operate on that data. We define a class by using the class keyword along with the class name, class MyClass { }
  • 4. 4 Object A class consists of a collection of fields, or variables, very much like the named fields of a struct ,all the operations (called methods) that can be performed on those fields. A class describes objects and operations defined on those objects. To create an object from a class, we type the class's name followed by the name of the object. For example, the line below creates an object from the MyClass class, MyClass myObject = new MyClass();
  • 5. 5 Declaring Fields for a Class We declare fields for class in much the same way as any variable in a program, by typing the data type of the field followed by the name of the field, like this: int name; Class fields that is data field is by default accessible only by methods in the same package. we can change the rules of this access by using the public, protected, and private keywords.
  • 6. 6 Public A public data field can be accessed by any part of a program, inside or outside of the class in which it's defined. Protected A protected data field can only be accessed from within the class or from within a derivedclass (a subclass). Private A private data field cannot even be accessed by a derived class.
  • 7. 7 Defining Methods Method describe behavior of an object. A method is a collection of statements that are group together to perform an operation. Syntax : return-type methodName(parameter-list) { //body of method }
  • 8. 8 void show() { System.out.println(“Welcome…”); } Here return type is void ,show is name. int getNo() { no=10; return no; } Here return type is int ,getNo is name.
  • 9. 9 Parameter is variable defined by a method that receives value when the method is called. Parameter are always local to the method they dont have scope outside the method. While argument is a value that is passed to a method when it is called.
  • 10. 10 There are two ways to pass an argument to a method call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments. call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the agrument value.
  • 11. 11 call-by-value public class A { int a; public void callByValue(int x) { a=x; System.out.println(a); } public static void main(String[] args) { A obj = new A(10); obj.callByValue(x); //function call } }
  • 12. 12 call-by-reference public class A { int x=10; int y=20; public void callByReference(A t) { t.x=100; t.y=50; } public static void main(String[] args) { A s = new A(); System.out.println("Before "+s.x+" "+s.y); s.callByReference(s); System.out.println("After "+s.x+" "+s.y); } }
  • 13. 13 Controlling Access to Members of a Class Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: 1.At the top level—public, or package-private (no explicit modifier). 2. At the member level—public, private, protected, or package-private (no explicit modifier).
  • 14. 14 A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier, it is visible only within its own package. The following table shows the access to members permitted by each modifier. Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N
  • 15. 15 Constructor Constructor is special type of method, enables an object to initialize itself when it's created. A constructor is a public method with the same name as the class. class A { int a; public A(int value) { a = value; } }
  • 16. 16 Class's constructor starts with the public keyword. This is important because we want to be able to create an object from the class anywhere in program, and when we create an object, you're actually calling its constructor. After the public keyword comes the name of the constructor followed by the constructor's arguments in parentheses. When you create an object of the class, you must also provide the required arguments.
  • 17. 17 There are two types of constructor Default Constructor Parameterized constructor Each time a new object is created at least one constructor will be invoked. A obj1=new A()//Default constructor invoked A obj2=new A() //Parameterized constructor invoked
  • 18. 18 Constructor Overloading Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters. class A { int a; public A() { a=0; } public A(int value) { a = value; } }
  • 19. 19 Using the this Keyword Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. we can refer to any member of the current object from within an instance method or a constructor by using this.
  • 20. 20 public class Point { public int x = 0; public int y = 0; public Point(int x, int y) { this.x = x; this.y = y; } } Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.
  • 21. 21 Static Same data is used for all the instances (objects) of some Class. Class A { public int y = 0; public static int x= 1; }; A a = new A(); A b = new A(); System.out.println(b.x); a.x = 5; System.out.println(b.x); A.x= 10; System.out.println(b.x); Assignment performed on the first access to the Class. Only one instance of ‘x’ exists in memory Output: 1 5 10 a b y y A.x_ 0 0 1
  • 22. 22 Static member function Static member function can access only static members Static member function can be called without an instance. Class TeaPot { private static int numOfTP = 0; private Color myColor_; public TeaPot(Color c) { myColor_ = c; numOfTP++; } public static int howManyTeaPots() { return numOfTP; } // error : public static Color getColor() { return myColor_; } }
  • 23. 23 TeaPot tp1 = new TeaPot(Color.RED); TeaPot tp2 = new TeaPot(Color.GREEN); System.out.println(“We have “ + TeaPot.howManyTeaPots()+ “Tea Pots”); Static Block Code that is executed in the first reference to the class. Several static blocks can exist in the same class ( Execution order is by the appearance order in the class definition ). Only static members can be accessed.
  • 24. 24 Class design hints 1.Always keep data private. 2.Always initialize data. 3.Don’t use too many basic type in class. 4.Not all fields indivisual field accessor and mutators. 5.Brak up classes that have too many responsibilities. 6.Make the name of your classes and method that reflect their responsibilty.
  翻译: