2
2
.
.
5
5
.
.
1
1
M
M
e
e
t
t
h
h
o
o
d
d
s
s
I
I
n
n
f
f
o
o
Keyword return (followed by the value) must be used if function returns some value.
Each function has fixed number of parameters (they all have to be given in function call and can't have default values).
Content
Methods Syntax (String greet (String name, int age) { return(name + " is " + age + " years old"); })
Overloaded Methods (Have the same name but different number and type of Parameters)
Overridden Methods (Parent's Method replaced by the Child's Method with the same name & Parameters)
M
M
e
e
t
t
h
h
o
o
d
d
S
S
y
y
n
n
t
t
a
a
x
x
Method is declared & implemented
by specifying Method return Data Type String (use void if there is no return value)
followed by the Method Name greet
followed by the Method Input Parameters (String name, int age)
followed by the Method Body { ... } (Without Body it is just declared)
Method Syntax
String greet (String name, int age) { return(name + " is " + age + " years old"); }
Method Syntax
public class Test {
//DECLARE & IMPLEMENT FUNCTION.
public static String greet (String name, int age) { return(name + " is " + age + " years old"); }
public static void main(String args[]) {
//CALL FUNCTION.
String result = greet("John", 20);
//DISPLAY RESULT.
System.out.print(result);
}
}
O
O
v
v
e
e
r
r
l
l
o
o
a
a
d
d
e
e
d
d
M
M
e
e
t
t
h
h
o
o
d
d
s
s
Overloaded methods have the same name but different number or type of parameters.
In this example Person constructor (method) is overloaded since two constructors have the same name but different
number and type of parameters.
Overloading
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Person john = new Person("John");
john.say();
Person jill = new Person("Jill", 25);
jill.say();
}
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//PROPERTIES.
String name;
int age;
//CONSTRUCTOR WITH ONE PARAMETER.
Person(String name) {
this.name = name;
}
//CONSTRUCTOR WITH TWO PARAMETERS.
Person(String name, int age) {
this.name = name;
this.age = age;
}
//METHOD.
void say() {
System.out.println(name + " is " + age);
}
}
O
O
v
v
e
e
r
r
r
r
i
i
d
d
d
d
e
e
n
n
M
M
e
e
t
t
h
h
o
o
d
d
s
s
When during Inheritance Child Class declares Method with the same Name and Parameters as an existing Parent's
Method then it is said that Parent's Method has been overridden (replaced) by the Child's Method.
Test.java
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Soldier john = new Soldier();
john.attack();
}
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
void attack() { System.out.println("Using fist."); }
}
//===========================================================================================================
// CLASS: Soldier
//===========================================================================================================
class Soldier extends Person {
void attack() { System.out.println("Using gun."); }
}