QA's approach 2 Java - Methods in Java
Methods in Java:
1. Used to expose the behaviour of the objects2. Used to perform an action and would or wouldn't return any outcome for that action.
Examples:
Click() ->Doesn't require any outcome of the action.
Display()-> Require the action to display an output to some terminal.
GetBalance()->Require the action to fetch the Balance of the customer.
Method signature:
[With or Without Return type] Method(with or without parameters)
{
-------------
---------------
[With or Without Return statement;]
}
Method Parameters can be of primitive, reference type, variable arguments or array types
1. method(Primitive type)
Display(Int a)
Display(Int a)
2. method(Reference type)
display(String s)
display(Student s1)
display(Student a[])
display(Object obj[])
3. method(Collection object)
4. method(String args...)
Variable no of arguments passing
Arbitrary Number of Arguments of same type use varargs when you don't know how many of a particular type of argument will be passed to the method.Static methods:
1. Class methods can access class variables and class methods directly.
2. Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
3. Behavior or logic which is not specific to an object.
When to use/choose static methods:
1. When the same behavior is shared among all the instances of the class. [Commonly utilized functionality]
2. When a method does not alter or utilize the state of the object [Data/values from the instance variables or non static fields].
Instance/Non static methods:
1. Instance methods can access instance variables and instance methods directly.2. Instance methods can access class variables and class methods directly.
3. Behavior or logic which is specific to an object.
When to use/choose Non static methods:
1. When different behavior is to be exhibited by individual instances of the class.
2. When a method needs to alter or utilize the state of the object [Data/values from the instance variables or non static fields].
class staticmethodEx{
static int a;
int b=15;
static result()
{
.......
......
}
calculate()
{
.......
......
}
static display()
{
result();
s.o.p(a);
calculate(); //error
s.o.p(b); //error
staticmethodEx obj=new staticmethodEx();
obj.calculate();
s.o.p(b);
......
}
}
Method Overloading:
Method called within a program is associated at compile time is static binding
Same method is implemented inn different child classes with different logic.
Example:
//Business logic class doesnt contain main method
class Car
{
drive()
{
S.o.p("Generic drive method")
}
}
//Business logic class doesnt contain main method
class Audi extends Car
{
drive()
{
S.o.p("Audi drive - High acceleration")
}
}
//Business logic class doesnt contain main method
class Nano extends Car
{
drive()
{
S.o.p("Nano drive - Low acceleration")
}
}
//Execution logic class contains main method
class Driver
{
public static void main(String args...)
{
Audi a1 =new Audi();
Car a2 =new Audi();
Car a3 =new Nano();
a1.drive();
a2.drive(); //overloads drive method of Car with drive method of Audi
a3.drive(); //overloads drive method of Car with drive method of Nano
//Reference variable is changed from a1 to a2 to a3
}
}
Method Overriding:
Method called within a program is associated at run time is Dynamic binding.
methods with same method name in same class but different signatures, hence same method performs different tasks
Example:
//Business logic class doesnt contain main method
class DragAndDrop
{
drag()
{
S.o.p("Generic drag method")
}
drag(Int x)
{
S.o.p("drag to specific location")
}
String drag(Int x, Int y)
{
Return "Drag successfull to specific location in x,y"
}
}
//Execution logic class contains main method
class Driver
{
public static void main(String args...)
{
DragAndDrop dg=new DragAndDrop();
dg.drag();
dg.drag(10);
dg.drag(10,20);
//overridden methods
}
}
Usage of 'this' keyword with methods
A method uses 'this' to refer to the instance of the class that is executing the method. Static methods do not use this
Comments
Post a Comment