Hey guys good evening
humne apni pichli post me seekha tha Java Constructor ke bare me or ab hum apni is post me seekhenge Java Methods ke
bare main. aayiye friends ab hum seedhe apne topic par chalte hain.
Java Methods Full Guide
In Hindi
Java Introduction In Hindi
Friends kabhi kabhi hume
kisi task ko apne program main kayi jgah perform karna padta hai. dosto agar
hum is task ko bina method ke karenge to hume ek hi code ko kayi bar repeat
karke jgah jgah likhna padega. dosto agar iske bjaye hum ek esa method bna le
jo is operation ko perform karta hai. dosto esa karne se hume ek hi code ko bar
bar likhne ki jarurt nahi hai. dosto hum program me jaha bhi vo task perform
karna chahte hain us jga us method ko call kar sakte hain.
Friends Method basically
ek block of statements hota hai. ye ek particular operation perform karta hai
dosto is operation ko hum apne program me jaha bhi perform karna chahte hain
waha method ka name likhkar use call karte hain or compiler us jgah method ko
execute kar deta hai.
Friends println() ko java
me method ka ek bhot hi accha example mana jata hai. dosto jesa ki hum sabhi
jante hain ki kisi bhi java program main hume is method ko bohut bar jarurat
padti hai. dosto ye method java library me define kiya gya hai. dosto is method
ka kaam hota hai ki hum iske koi bhi string pass karke usko screen par print
karwa sakte hain. dosto iske saath hi hum is method main variable ko pass karke
unki values hi print karwa sakte hain. dosto jab bhi hume is method ki jarurat
hoti hai to hum isko call karte hain or isme string ya variables pass karte
hain or ye method usi jgah par execute hota hai.
Dosto agar hum java
library main is method ki definition dekhenge to hum pta chalega ki hum us code
ko bar bar apne program me nhi likh sakte hain or agar hum us code ko use karte
hain to humara first hello world program 10 line se jada ka ho jayega.
Dosto methods program main
code ko reduce karne or computer ki memory bachane ka accha medium hai saath hi
methods ke use se programmers ko bhi same code bar bar nhi likhna padta hai
dosto isse programmer ka time bhi bachta hai.
Structure of Method
Definition In Hindi
access_specifier
return_type method_name (parameters list)
{
// statements
to be executed
}
Syntax
|
Explanation
|
access specifier
|
Access specifier show karta hain ki ye method kis trah ka hai. Jaise ki private,
public ya protected
etc.
|
return type
|
Method ka execution hone ke baad method
result ke roop main kis
trah ki value return karega
ye hum yaha define karte
hain agar humara method koi value
return nhi karta to hum
yaha par void likhte
hain.
|
method name
|
ye method ka name hota hai. Ye pure program main unique hona chahiye.
|
parameters list
|
Ye vo variables ya values hoti hain jo hum method ko call karte time pass karte hain. Method par operation
perform karta hai or result
return karta hai. Ya fir
apne execution ke
liye inka use karta hai. Jaise
agar koi addition ka method banaye to usme 2 numbers
pass kar sakte hain jinko add karke method hume result return karega. hum values or variables ko comma se separate karte hain.
|
Method Call of Structure
In Hindi
If in the same class
|
If in another class
|
methodName(arg1,arg2….);
|
objectName.methodName(arg1,arg2);
|
Example of Java Methods In
Hindi
class PersonsInfo
{
public void
Separator()// our method which separates information of persons
{
System.out.println("-------------------------------------------------------------");
}
public
static void main(String args[])
{
System.out.println("Name : Tina Age : 22");
Separator();
System.out.println("Name : Raj Age:23");
Separator();
System.out.println("Name : Hina Age : 22");
Separator();
System.out.println("Name : Rock
Age:23");
}
}
Method Overloading In
Hindi
Friends jab hum bade
software's develop karte hain to aisa akshar hota hai ki humare program main ek
jesa kaam karne wale kayi methods hote hain. dosto in sabhi methods ka name
yaad rakhna or unhe use karna bada mushkil ho jata hai. jaise ki hum alag alag
trah ke data ko add karna chahte hain to uske liye hum alag alag methods
banayenge. fir un methods ko use karne ke liye hume unka name bhi
memorize karna hoga. kisi chhote se program main aisa hum easily kar
sakte hain. but jab hum koi bada software develop kar hote hain to hum sabhi
methods ke name yaad nhi kar sakte hain. is situation main hum method
overloading use kar sakte hain.
Method overloading main jo
methods ek jaisa task perform kar rahe hain un sabko hum ek hi name dete hain
jaise ki alag alag addition ke methods addition hi kar rahe hain. but koi
integer ko add kar rha hai to koi double ko add kar rha hai in sabka ek
hi name diya jata hai. jab hum pooch sakte hain ki agar ek jaisa name rakhenge
to compiler ko pta kaise chalega ki hum konsa method call karna chahte hain.
In sabhi methods ka name
same hota hai lekin in methods main parameter types or unki sankhya alag alag
hoti hai jisse compiler main inhe run time par ye pta chal jata hai ki konsa
method execute karna hai. jis method se arguments ho jate hain wahi method
execute ho jata hai.
Example
public class overDemo
{
public
static int sum(int a, int b)
{
return a+b;
}
public
static int sum(double x, double y)
{
return x+y;
}
public
static void main(String args [])
{
System.out.println(sum(3,5));
System.out.println(sum(3.5,5.3));
}
}
Friends uper diye gye
example main 2 sum methods hain. ek sum() method integers ko add karta hai or
dusra double values ko add karta hai. method call ke duaraan agar sum() method
main integers pass karte hain to integers wala sum() method execute hoga. or
agar double values pass karte hain to double wala sum() method execute hoga.
Method overloading In
Hindi
Friends jab hum kisi class
ko inherit karte hain to hum uske sare methods ko access kar sakte hain. but
kya hoga agar kisi method ki definition humari sub class ke according nahi hai.
ya fir hum us method main kuch change karna chahte hain. agar aisa hai to hum
super class ke kisi bhi method ko sub class main dobara define kar sakte hain.
hum us method ko edit kar sakte hain taki vo sub class ke liye work kar sake.
ise method overriding kehte hain.
Friends Method overriding
main sub class super class wala method use nahi karna chahti hai or vo khud us
method ko edit karke use karna chahti hai. aisa karke vo super class ke method
ko override karti hai.
For example agar ek class
hai or usme display() method hai. is class ko circle class inherit kar rahi
hai. circle class area class ke display method ko use karne ki wajaye us method
ko circle class ki aawashyakta ke anushar badal kar wapas kar sakti hai.
Friends jab hum kisi
method ko override karte hain to hume hamesha 2 baate yaad rakhni chahiye.
1- Super class ke method
ka or sub class ke method ka name same hoga.
2- Sub class ki parameter
list super class ki parameter list se match honi chahiye.
Example
class Area
{
public void display()
{
System.out.println(“Displaying area……..”);
}
}
class circle extends Area
{
public
void display()
{
System.out.println(“Displaying circle…….”);
}
public
static void main(String args[])
{
Area obj = new Area();
obj.display();
this.display();
}
}
Final Words
To friends ye thi humari
aaj ki post jisme humne java methods ke bare main seekha or jana. dosto aapke
is post se related koi bhi questions hain to aap hume comment box main comment
karke puch sakte hain. friends isi ke saath saath aap hume apne suggestions bhi
bta sakte hain. or dosto post ko share jarur kijiye Thanks
Please Share This Post
Very nic article keep growing. for more article's like this and other study material, programmig language free training, latest tips and tricks, sports and politics news and much more visit to https://searchofia.blogspot.in/
ReplyDeletefor more java education go to https://javarevisited.blogspot.in/
ReplyDelete