SlideShare a Scribd company logo
Java Methods
New Wave Analytica
We Find Solutions in Data
Outline
▪ Class Instance Methods
▪ Static Methods
▪ Methods with no Parameters
▪ Methods with Parameters
▪ Methods with Return Value
▪ Method Overloading
▪ Method Overriding
New Wave Analytica
Class Instance Methods
▪ Instance methods require an object of its class to be created
before it can be called.
▪ To invoke an instance method, create an object of the class
within which it is defined.
New Wave Analytica
Class Instance Methods
▪ Instance methods require an object of its class to be created before it can
be called.
▪ To invoke an instance method, create an object of the class within which it
is defined.
▪ Instance methods belong to the object of the class not to the class, they
can be called after creating the object of the class.
▪ Every individual object create from the class has its own copy of the
instance methods of that class.
▪ Every individual Object created from the class has its own copy of the
instance method(s) of that class.
New Wave Analytica
Example
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StudentRunner {
public static void main(String[] args) {
Student student = new Student();
student.setName("Me");
student.getName();
}
}
New Wave Analytica
Static Method
▪Static methods are the methods in Java that can be called without creating
an object of class.
▪ They are referenced by the class name itself or reference to the object of
that class.
▪ Static methods are associated to the class in which they reside i.e. they can
be called even without creating an instance of the class, i.e
ClassName.methodName(args).
▪ They are designed with aim to be shared among all objects created from
the same class.
New Wave Analytica
Example
class Student{
public static String studentName = "";
public static void getName(String name){
studentName = name;
}
}
public class StudentRunner {
public static void main (String[] args) {
// Accessing the static method getName() and
// field by class name itself.
Student.getName("Me");
System.out.println(Student.studentName);
// Accessing the static method getName() by using Object's
reference.
Student student = new Student();
student.getName("You");
System.out.println(student.studentName);
}
}
New Wave Analytica
Method with no Parameter
▪ Method that does not accept any arguments.
public class MathDemo {
void areaOfcircle() {
System.out.print("enter the radius :");
Scanner s = new Scanner(System.in);
float r = s.nextFloat();
float ar;
ar = (r * r) * 22 / 7;
System.out.println("area of the circle is :
"+ar+" sq units.");
} }
public class MathDemoRunner {
public static void main(String[] args) {
MathDemo obj = new MathDemo();
obj.areaOfCircle();
} }
New Wave Analytica
Method with Parameters
▪ Method that accepts arguments.
public class MathDemo {
void areaOfCircle(float r, float ar)
{
ar = (r * r) * 22 / 7;
System.out.println("area of the circle is : "+ar+"
sq units.");
}
}
public static void main(String[] args) {
MathDemo obj = new MathDemo();
obj.areaOfCircle(12, 12);
}
New Wave Analytica
Methods with Return Value
▪ Java requires that a method declare the data type of the value that it returns.
▪ If a method does not return a value, it must be declared to return void.
public class MathDemo {
static int myMethod(int x) {
return 5 + x;
}
public static void main(String[] args) {
System.out.println(myMethod(3));
}
}
New Wave Analytica
Method Overloading
▪ Method overloading
▪ allows a method with the same name but different parameters, to have different
implementations and return values of different types
▪ can be used when the same operation has different implementations.
▪ Overloaded methods have the following properties:
▪ the same name
▪ different parameters
▪ return types can be different or the same
New Wave Analytica
Method Overloading
public class MathDemo {
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y) {
return (x + y);
}
// Overloaded sum(). This sum takes three int
parameters
public int sum(int x, int y, int z) {
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
parameters
public double sum(double x, double y) {
return (x + y); }
// Driver code
public static void main(String args[]) {
MathDemo s = new MathDemo();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
New Wave Analytica
Method Overriding
▪ Method Overriding in Java is a condition when a subclass has the same method as
declared in the parent class.
▪ A parent class can be called an overridden method.
▪ In object-oriented programming, the feature of overriding is used to provide a class,
subclass or a child class to use a method that is already used by parent class to have a
specific implementation.
▪ Method overriding in Java programming occurs when the method in the subclass has the
same return type, or parameters, name or signature as the parent class.
▪ Method overriding is the method by which Java can support runtime polymorphism.
▪ Basically, the method to execute is chosen on the basis of the type of object and not on
the type of reference variable.
New Wave Analytica
Method Overriding
public class Parent {
void methodOfParentClass() {
System.out.println("Parent's method()");
} }
public class Child extends Parent {
@Override
void methodOfParentClass() {
System.out.println("Child's method()");
} }
public class MethodOverriding {
public static void main(String[] args) {
Parent obj1 = new Parent();
obj1.methodOfParentClass();
Parent obj2 = new Child();
obj2.methodOfParentClass();
}
}
New Wave Analytica

More Related Content

What's hot (20)

PPTX
Class or Object
Rahul Bathri
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PDF
Lect 1-java object-classes
Fajar Baskoro
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Class introduction in java
yugandhar vadlamudi
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
6. static keyword
Indu Sharma Bhardwaj
 
PPT
Java lec class, objects and constructors
Jan Niño Acierto
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Classes&objects
M Vishnuvardhan Reddy
 
PDF
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
PPT
11 Using classes and objects
maznabili
 
PPTX
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
PPS
Introduction to class in java
kamal kotecha
 
PPT
Class and object in C++
rprajat007
 
PPT
9781439035665 ppt ch08
Terry Yoast
 
PDF
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
PPT
Object and Classes in Java
backdoor
 
PPTX
Week10 packages using objects in objects
kjkleindorfer
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Class or Object
Rahul Bathri
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Lect 1-java object-classes
Fajar Baskoro
 
Class and Objects in Java
Spotle.ai
 
Class introduction in java
yugandhar vadlamudi
 
Pi j3.2 polymorphism
mcollison
 
6. static keyword
Indu Sharma Bhardwaj
 
Java lec class, objects and constructors
Jan Niño Acierto
 
Classes, objects in JAVA
Abhilash Nair
 
Classes&objects
M Vishnuvardhan Reddy
 
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
11 Using classes and objects
maznabili
 
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
Introduction to class in java
kamal kotecha
 
Class and object in C++
rprajat007
 
9781439035665 ppt ch08
Terry Yoast
 
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
Object and Classes in Java
backdoor
 
Week10 packages using objects in objects
kjkleindorfer
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 

Similar to Java Methods (20)

PDF
Inheritance and interface
Rosmina Joy Cabauatan
 
PDF
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
PDF
Oriented Programming Concepts and Principles
Rosmina Joy Cabauatan
 
PDF
Web Technology-Method .pdf
nandiaditi2010
 
PPT
Ch. 6 -Static Class Members.ppt programming
adamjarrah2006
 
PPTX
Functions & Methods in Java: A Beginner's Guide.pptx
Chris Lewis
 
PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
PPTX
Object oriented concepts
Gousalya Ramachandran
 
PPTX
IntroductionJava Programming - Math Class
sandhyakiran10
 
PPTX
Class and Object.pptx from nit patna ece department
om2348023vats
 
PPTX
Chapter 5:Understanding Variable Scope and Class Construction
It Academy
 
PPTX
Chap2 class,objects contd
raksharao
 
PDF
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
PPTX
Chapter 6 Methods.pptx
ssusere3b1a2
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
PPTX
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
PPTX
OOPSCA1.pptx
Soumyadipchanda2
 
Inheritance and interface
Rosmina Joy Cabauatan
 
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
Oriented Programming Concepts and Principles
Rosmina Joy Cabauatan
 
Web Technology-Method .pdf
nandiaditi2010
 
Ch. 6 -Static Class Members.ppt programming
adamjarrah2006
 
Functions & Methods in Java: A Beginner's Guide.pptx
Chris Lewis
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
Object oriented concepts
Gousalya Ramachandran
 
IntroductionJava Programming - Math Class
sandhyakiran10
 
Class and Object.pptx from nit patna ece department
om2348023vats
 
Chapter 5:Understanding Variable Scope and Class Construction
It Academy
 
Chap2 class,objects contd
raksharao
 
Class and Object JAVA PROGRAMMING LANG .pdf
sameer2543ynr
 
Chapter 6 Methods.pptx
ssusere3b1a2
 
Polymorphism in java
Elizabeth alexander
 
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
MODULE_3_Methods and Classes Overloading.pptx
VeerannaKotagi1
 
OOPSCA1.pptx
Soumyadipchanda2
 
Ad

Recently uploaded (20)

PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Ad

Java Methods

  • 1. Java Methods New Wave Analytica We Find Solutions in Data
  • 2. Outline ▪ Class Instance Methods ▪ Static Methods ▪ Methods with no Parameters ▪ Methods with Parameters ▪ Methods with Return Value ▪ Method Overloading ▪ Method Overriding New Wave Analytica
  • 3. Class Instance Methods ▪ Instance methods require an object of its class to be created before it can be called. ▪ To invoke an instance method, create an object of the class within which it is defined. New Wave Analytica
  • 4. Class Instance Methods ▪ Instance methods require an object of its class to be created before it can be called. ▪ To invoke an instance method, create an object of the class within which it is defined. ▪ Instance methods belong to the object of the class not to the class, they can be called after creating the object of the class. ▪ Every individual object create from the class has its own copy of the instance methods of that class. ▪ Every individual Object created from the class has its own copy of the instance method(s) of that class. New Wave Analytica
  • 5. Example public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class StudentRunner { public static void main(String[] args) { Student student = new Student(); student.setName("Me"); student.getName(); } } New Wave Analytica
  • 6. Static Method ▪Static methods are the methods in Java that can be called without creating an object of class. ▪ They are referenced by the class name itself or reference to the object of that class. ▪ Static methods are associated to the class in which they reside i.e. they can be called even without creating an instance of the class, i.e ClassName.methodName(args). ▪ They are designed with aim to be shared among all objects created from the same class. New Wave Analytica
  • 7. Example class Student{ public static String studentName = ""; public static void getName(String name){ studentName = name; } } public class StudentRunner { public static void main (String[] args) { // Accessing the static method getName() and // field by class name itself. Student.getName("Me"); System.out.println(Student.studentName); // Accessing the static method getName() by using Object's reference. Student student = new Student(); student.getName("You"); System.out.println(student.studentName); } } New Wave Analytica
  • 8. Method with no Parameter ▪ Method that does not accept any arguments. public class MathDemo { void areaOfcircle() { System.out.print("enter the radius :"); Scanner s = new Scanner(System.in); float r = s.nextFloat(); float ar; ar = (r * r) * 22 / 7; System.out.println("area of the circle is : "+ar+" sq units."); } } public class MathDemoRunner { public static void main(String[] args) { MathDemo obj = new MathDemo(); obj.areaOfCircle(); } } New Wave Analytica
  • 9. Method with Parameters ▪ Method that accepts arguments. public class MathDemo { void areaOfCircle(float r, float ar) { ar = (r * r) * 22 / 7; System.out.println("area of the circle is : "+ar+" sq units."); } } public static void main(String[] args) { MathDemo obj = new MathDemo(); obj.areaOfCircle(12, 12); } New Wave Analytica
  • 10. Methods with Return Value ▪ Java requires that a method declare the data type of the value that it returns. ▪ If a method does not return a value, it must be declared to return void. public class MathDemo { static int myMethod(int x) { return 5 + x; } public static void main(String[] args) { System.out.println(myMethod(3)); } } New Wave Analytica
  • 11. Method Overloading ▪ Method overloading ▪ allows a method with the same name but different parameters, to have different implementations and return values of different types ▪ can be used when the same operation has different implementations. ▪ Overloaded methods have the following properties: ▪ the same name ▪ different parameters ▪ return types can be different or the same New Wave Analytica
  • 12. Method Overloading public class MathDemo { // Overloaded sum(). // This sum takes two int parameters public int sum(int x, int y) { return (x + y); } // Overloaded sum(). This sum takes three int parameters public int sum(int x, int y, int z) { return (x + y + z); } // Overloaded sum(). This sum takes two double parameters public double sum(double x, double y) { return (x + y); } // Driver code public static void main(String args[]) { MathDemo s = new MathDemo(); System.out.println(s.sum(10, 20)); System.out.println(s.sum(10, 20, 30)); System.out.println(s.sum(10.5, 20.5)); } } New Wave Analytica
  • 13. Method Overriding ▪ Method Overriding in Java is a condition when a subclass has the same method as declared in the parent class. ▪ A parent class can be called an overridden method. ▪ In object-oriented programming, the feature of overriding is used to provide a class, subclass or a child class to use a method that is already used by parent class to have a specific implementation. ▪ Method overriding in Java programming occurs when the method in the subclass has the same return type, or parameters, name or signature as the parent class. ▪ Method overriding is the method by which Java can support runtime polymorphism. ▪ Basically, the method to execute is chosen on the basis of the type of object and not on the type of reference variable. New Wave Analytica
  • 14. Method Overriding public class Parent { void methodOfParentClass() { System.out.println("Parent's method()"); } } public class Child extends Parent { @Override void methodOfParentClass() { System.out.println("Child's method()"); } } public class MethodOverriding { public static void main(String[] args) { Parent obj1 = new Parent(); obj1.methodOfParentClass(); Parent obj2 = new Child(); obj2.methodOfParentClass(); } } New Wave Analytica