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

PPTX
constructors in java ppt
kunal kishore
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
class and objects
Payel Guria
 
PDF
Files in java
Muthukumaran Subramanian
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
I/O Streams
Ravi Chythanya
 
PPTX
Constructor in java
Pavith Gunasekara
 
constructors in java ppt
kunal kishore
 
This keyword in java
Hitesh Kumar
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
class and objects
Payel Guria
 
Files in java
Muthukumaran Subramanian
 
Constructor in java
Madishetty Prathibha
 
I/O Streams
Ravi Chythanya
 
Constructor in java
Pavith Gunasekara
 

What's hot (20)

PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Inheritance In Java
Manish Sahu
 
PPT
Inheritance in java
Lovely Professional University
 
PPT
Method overriding
Azaz Maverick
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
Object oriented programming with python
Arslan Arshad
 
PPTX
java interface and packages
VINOTH R
 
PPTX
Constructor overloading & method overloading
garishma bhatia
 
PPTX
Constructor in java
SIVASHANKARIRAJAN
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Data types in php
ilakkiya
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPT
Packages in java
Abhishek Khune
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
L21 io streams
teach4uin
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
PDF
Java IO
UTSAB NEUPANE
 
Classes, objects in JAVA
Abhilash Nair
 
Polymorphism in java
Elizabeth alexander
 
Inheritance In Java
Manish Sahu
 
Inheritance in java
Lovely Professional University
 
Method overriding
Azaz Maverick
 
Strings in Java
Abhilash Nair
 
Object oriented programming with python
Arslan Arshad
 
java interface and packages
VINOTH R
 
Constructor overloading & method overloading
garishma bhatia
 
Constructor in java
SIVASHANKARIRAJAN
 
Applets in java
Wani Zahoor
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Data types in php
ilakkiya
 
Python Modules
Nitin Reddy Katkam
 
Packages in java
Abhishek Khune
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
L21 io streams
teach4uin
 
Data Types & Variables in JAVA
Ankita Totala
 
Java IO
UTSAB NEUPANE
 
Ad

Viewers also liked (20)

PPTX
OOP - Java is pass-by-value
Mudasir Qazi
 
PPT
8. String
Nilesh Dalvi
 
PPT
6. Exception Handling
Nilesh Dalvi
 
PPT
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
PPT
7. Multithreading
Nilesh Dalvi
 
PPT
14. Linked List
Nilesh Dalvi
 
PPT
Input output streams
Parthipan Parthi
 
PPT
3. Data types and Variables
Nilesh Dalvi
 
PPT
11. Arrays
Nilesh Dalvi
 
PPT
10. Introduction to Datastructure
Nilesh Dalvi
 
PPT
13. Queue
Nilesh Dalvi
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
2CPP08 - Overloading and Overriding
Michael Heron
 
PPTX
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
PPT
12. Stack
Nilesh Dalvi
 
PDF
Built in classes in java
Mahmoud Ali
 
PDF
Java Course 8: I/O, Files and Streams
Anton Keks
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PPTX
Presentation on overloading
Charndeep Sekhon
 
PDF
Methods in Java
Jussi Pohjolainen
 
OOP - Java is pass-by-value
Mudasir Qazi
 
8. String
Nilesh Dalvi
 
6. Exception Handling
Nilesh Dalvi
 
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
7. Multithreading
Nilesh Dalvi
 
14. Linked List
Nilesh Dalvi
 
Input output streams
Parthipan Parthi
 
3. Data types and Variables
Nilesh Dalvi
 
11. Arrays
Nilesh Dalvi
 
10. Introduction to Datastructure
Nilesh Dalvi
 
13. Queue
Nilesh Dalvi
 
9. Input Output in java
Nilesh Dalvi
 
2CPP08 - Overloading and Overriding
Michael Heron
 
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
12. Stack
Nilesh Dalvi
 
Built in classes in java
Mahmoud Ali
 
Java Course 8: I/O, Files and Streams
Anton Keks
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Presentation on overloading
Charndeep Sekhon
 
Methods in Java
Jussi Pohjolainen
 
Ad

Similar to 4. Classes and Methods (20)

PPTX
Java As an OOP Language,Exception Handling & Applets
Helen SagayaRaj
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PDF
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
PPTX
UNIT - IIInew.pptx
akila m
 
PDF
Java keywords
Ravi_Kant_Sahu
 
PDF
Keywords and classes
Ravi_Kant_Sahu
 
PPTX
Java chapter 5
Abdii Rashid
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPTX
Android Training (Java Review)
Khaled Anaqwa
 
PPTX
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
PDF
Java defining classes
Mehdi Ali Soltani
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPT
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
PPTX
Introduction to java programming
shinyduela
 
PPT
Lecture 2 classes i
the_wumberlog
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
PDF
ITFT-Classes and object in java
Atul Sehdev
 
Java As an OOP Language,Exception Handling & Applets
Helen SagayaRaj
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
UNIT - IIInew.pptx
akila m
 
Java keywords
Ravi_Kant_Sahu
 
Keywords and classes
Ravi_Kant_Sahu
 
Java chapter 5
Abdii Rashid
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Android Training (Java Review)
Khaled Anaqwa
 
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Java defining classes
Mehdi Ali Soltani
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
Introduction to java programming
shinyduela
 
Lecture 2 classes i
the_wumberlog
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
ITFT-Classes and object in java
Atul Sehdev
 

More from Nilesh Dalvi (14)

PPT
2. Basics of Java
Nilesh Dalvi
 
PPT
1. Overview of Java
Nilesh Dalvi
 
PPT
Standard Template Library
Nilesh Dalvi
 
PPT
Templates
Nilesh Dalvi
 
PPT
File handling
Nilesh Dalvi
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPT
Strings
Nilesh Dalvi
 
PPT
Polymorphism
Nilesh Dalvi
 
PPT
Inheritance : Extending Classes
Nilesh Dalvi
 
PPT
Operator Overloading
Nilesh Dalvi
 
PDF
Constructors and destructors
Nilesh Dalvi
 
PDF
Classes and objects
Nilesh Dalvi
 
PDF
Introduction to cpp
Nilesh Dalvi
 
PDF
Introduction to oops concepts
Nilesh Dalvi
 
2. Basics of Java
Nilesh Dalvi
 
1. Overview of Java
Nilesh Dalvi
 
Standard Template Library
Nilesh Dalvi
 
Templates
Nilesh Dalvi
 
File handling
Nilesh Dalvi
 
Input and output in C++
Nilesh Dalvi
 
Strings
Nilesh Dalvi
 
Polymorphism
Nilesh Dalvi
 
Inheritance : Extending Classes
Nilesh Dalvi
 
Operator Overloading
Nilesh Dalvi
 
Constructors and destructors
Nilesh Dalvi
 
Classes and objects
Nilesh Dalvi
 
Introduction to cpp
Nilesh Dalvi
 
Introduction to oops concepts
Nilesh Dalvi
 

Recently uploaded (20)

PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
BÀI TáșŹP TEST BỔ TRỹ THEO Tá»ȘNG CHỊ ĐỀ CỊA Tá»ȘNG UNIT KÈM BÀI TáșŹP NGHE - TIáșŸNG A...
Nguyen Thanh Tu Collection
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
BÀI TáșŹP TEST BỔ TRỹ THEO Tá»ȘNG CHỊ ĐỀ CỊA Tá»ȘNG UNIT KÈM BÀI TáșŹP NGHE - TIáșŸNG A...
Nguyen Thanh Tu Collection
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 

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