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

Java Methods

  • 1.
    Java Methods New WaveAnalytica We Find Solutions in Data
  • 2.
    Outline ▪ Class InstanceMethods ▪ 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 methodsare 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 staticString 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 noParameter ▪ 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 ReturnValue ▪ 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 ▪ Methodoverloading ▪ 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 classMathDemo { // 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 ▪ MethodOverriding 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 classParent { 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