SlideShare a Scribd company logo
1 of 16
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

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

What's hot (20)

Java IO
Java IOJava IO
Java IO
 
Methods in java
Methods in javaMethods in java
Methods in java
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Java swing
Java swingJava swing
Java swing
 
Applets
AppletsApplets
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
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Applets in java
Applets in javaApplets in java
Applets in java
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Thread
ThreadThread
Thread
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 

Viewers also liked

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

Viewers also liked (19)

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
 
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
 
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
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 

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

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.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