SlideShare a Scribd company logo
Classes and MethodsClasses and Methods
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Java and Data StructuresJava and Data Structures
Classes in Java
• A class is a user-defined data type with a template
that serves to define its properties.
• A class is declared by use of the class keyword.
• General form of class is:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class classname
{
type instance-variable1;
type instance-variable2;
//
type instance-variableN;
type methodName(parameter-list)
{
//body of method
}
}
Classes in Java
Field Declaration:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length;
int width;
}
Classes in Java
Method Declaration:
• Methods are declared inside the body of the class but
immediately after the declaration of instance variable.
• The general form of method declaration id,
• Method declaration have four basic parts:
– Name of the method (methodName)
– Type of the value method return (type)
– List of parameters (parameter - List)
– Body of the method
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
type methodName (parameter-list)
{
method-body;
}
Classes in Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
void getData (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
Creating objects
• Object is a block of memory that contains space to store all
the instance variable.
• Creating an objects is also called as instantiating an objects.
• Objects are created using new keyword.
• new operator creates an objects of the specified class and
returns a reference to that object.
• Example:
Rectangle rect; // Declare the objects
rect = new Rectangle(); // instantiating the objects
• Both statement combined into one as follows,
Rectangle rect = new Rectangle();
• Each objects has its own copy of instance variable.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Accessing class members
• Using (.) operator we can access the class members i.e.
properties of class.
objectname.variablename = values;
objectname.methodname(parameterlist);
• Example:
rect.length = 10;
rect.width = 20;
rect.getData(10, 20);
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Classes in Java
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
void getData (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
class RectArea
{
public static void main (String arg[])
{
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
rect1.length = 10;
rect1.width = 10;
rect2.getData(10, 20);
System.out.println("Area 1: "+rect1.rectArea());
System.out.println("Area 2: "+ rect2.rectArea());
}
}
Constructors
• Constructors is a special type of method in java
which enables an object to initialize itself when it is
created.
• Constructors name is same as class name.
• They do not specify return type, not even void.
because it return the instance of class itself.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class Rectangle
{
int length, width; // combined declaration
Rectangle (int x, int y)
{
length = x;
width = y;
}
int rectArea ()
{
int area = length*width;
return (area);
}
}
class RectArea
{
public static void main (String arg[])
{
Rectangle rect = new (10, 20);
System.out.println("Area 1: "+ rect.rectArea());
}
}
this keyword
• this keyword can be used inside any method to refer
to the current class instance variable.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Rectangle (int length, int
width)
{
this.length = length;
this.width = width;
}
Method Overloading
• it is possible to create method that have the same
name, but different parameter lists and different
definitions.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Nesting Method
• A method can be called only by its method name by
another method of same class.
• This is known as nesting of method.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Static Members
• Like, static variable we can define static methods.
• In Java library, Math class defines many static
methods to perform math operation that can be
used in any program.
float x = Math.sqrt(25.0);
• Static methods called using class name.
• Static methods have several restriction:
– They can only call other static methods.
– They can only access static data.
– They can not refer to this or super in any program.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Static Members
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class StaticDemo
{
static float mul(float x, float y)
{
return x*y;
}
static float divide(float x, float y)
{
return x/y;
}
}
class StaticMain
{
public static void main(String arg [])
{
float a = StaticDemo.mul(4.0, 5.0);
float b = StaticDemo.divide(a, 2.0);
System.out.println("Value of b: "+b);
}
}
Q & A

More Related Content

What's hot

Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 

What's hot (20)

Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
L11 array list
L11 array listL11 array list
L11 array list
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Data types in java
Data types in javaData types in java
Data types in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 

Viewers also liked

Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 

Viewers also liked (20)

OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-value
 
8. String
8. String8. String
8. String
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
14. Linked List
14. Linked List14. Linked List
14. Linked List
 
Input output streams
Input output streamsInput output streams
Input output streams
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
13. Queue
13. Queue13. Queue
13. Queue
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
12. Stack
12. Stack12. Stack
12. Stack
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 

Similar to 4. Classes and Methods

Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 

Similar to 4. Classes and Methods (20)

Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
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
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Unit 3
Unit 3Unit 3
Unit 3
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Lecture 2 classes i
Lecture 2 classes iLecture 2 classes i
Lecture 2 classes i
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
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
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Chap11
Chap11Chap11
Chap11
 

More from Nilesh Dalvi

More from Nilesh Dalvi (14)

2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Templates
TemplatesTemplates
Templates
 
File handling
File handlingFile handling
File handling
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 

4. Classes and Methods

  • 1. Classes and MethodsClasses and Methods By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and Data StructuresJava and Data Structures
  • 2. Classes in Java • A class is a user-defined data type with a template that serves to define its properties. • A class is declared by use of the class keyword. • General form of class is: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class classname { type instance-variable1; type instance-variable2; // type instance-variableN; type methodName(parameter-list) { //body of method } }
  • 3. Classes in Java Field Declaration: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Rectangle { int length; int width; }
  • 4. Classes in Java Method Declaration: • Methods are declared inside the body of the class but immediately after the declaration of instance variable. • The general form of method declaration id, • Method declaration have four basic parts: – Name of the method (methodName) – Type of the value method return (type) – List of parameters (parameter - List) – Body of the method Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). type methodName (parameter-list) { method-body; }
  • 5. Classes in Java Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Rectangle { int length, width; // combined declaration void getData (int x, int y) { length = x; width = y; } int rectArea () { int area = length*width; return (area); } }
  • 6. Creating objects • Object is a block of memory that contains space to store all the instance variable. • Creating an objects is also called as instantiating an objects. • Objects are created using new keyword. • new operator creates an objects of the specified class and returns a reference to that object. • Example: Rectangle rect; // Declare the objects rect = new Rectangle(); // instantiating the objects • Both statement combined into one as follows, Rectangle rect = new Rectangle(); • Each objects has its own copy of instance variable. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Accessing class members • Using (.) operator we can access the class members i.e. properties of class. objectname.variablename = values; objectname.methodname(parameterlist); • Example: rect.length = 10; rect.width = 20; rect.getData(10, 20); Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Classes in Java Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Rectangle { int length, width; // combined declaration void getData (int x, int y) { length = x; width = y; } int rectArea () { int area = length*width; return (area); } } class RectArea { public static void main (String arg[]) { Rectangle rect1 = new Rectangle(); Rectangle rect2 = new Rectangle(); rect1.length = 10; rect1.width = 10; rect2.getData(10, 20); System.out.println("Area 1: "+rect1.rectArea()); System.out.println("Area 2: "+ rect2.rectArea()); } }
  • 9. Constructors • Constructors is a special type of method in java which enables an object to initialize itself when it is created. • Constructors name is same as class name. • They do not specify return type, not even void. because it return the instance of class itself. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 10. Constructor Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class Rectangle { int length, width; // combined declaration Rectangle (int x, int y) { length = x; width = y; } int rectArea () { int area = length*width; return (area); } } class RectArea { public static void main (String arg[]) { Rectangle rect = new (10, 20); System.out.println("Area 1: "+ rect.rectArea()); } }
  • 11. this keyword • this keyword can be used inside any method to refer to the current class instance variable. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Rectangle (int length, int width) { this.length = length; this.width = width; }
  • 12. Method Overloading • it is possible to create method that have the same name, but different parameter lists and different definitions. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. Nesting Method • A method can be called only by its method name by another method of same class. • This is known as nesting of method. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 14. Static Members • Like, static variable we can define static methods. • In Java library, Math class defines many static methods to perform math operation that can be used in any program. float x = Math.sqrt(25.0); • Static methods called using class name. • Static methods have several restriction: – They can only call other static methods. – They can only access static data. – They can not refer to this or super in any program. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 15. Static Members Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). class StaticDemo { static float mul(float x, float y) { return x*y; } static float divide(float x, float y) { return x/y; } } class StaticMain { public static void main(String arg []) { float a = StaticDemo.mul(4.0, 5.0); float b = StaticDemo.divide(a, 2.0); System.out.println("Value of b: "+b); } }
  • 16. Q & A