Skip to main content

Java Methods

Introduction to Java Methods 

कई बार ऐसा होता है की कोई एक काम या कोई एक task आपको program में कई जगह perform करना होता है। यदि आप इस काम को बिना methods के करेंगे तो आपको एक ही code बार बार कई जगह पर लिखना पड़ेगा। इसके बजाय यदि आप एक ऐसा method बना ले जो इस operation को perform करता है।
ऐसा करने से आपको एक ही code बार लिखने की जरुरत नहीं है। आप प्रोग्राम में जँहा भी वह task perform करना चाहते है, उस जगह उस method को call कर सकते है। 
Method basically एक block of statements होता है। ये block एक particular operation perform करता है। इस operation को आप program में जँहा भी perform करना चाहते है वँहा method का नाम लिखकर उसे call करते है। और compiler उस जगह method को execute कर देता है। 
Java में methods का बेहतरीन उदाहरण println() method है। क्या आप बता सकते है की किसी जावा program में आपको इस method की कितनी बार आवश्यकता पड़ती है?? जी हाँ बहुत बार पड़ती है। ये method असल में java library में define किया गया है।
इस method का काम ये होता है की आप इसके कोई भी string पास करके उसको screen पर print करवा सकते है। साथ ही आप इस method में variables को pass करके उनकी values भी print करवा सकते है। जब भी आपको इस method की जरुरत होती है तो आप इसको call करते है और इसमें string या variables पास करते है। और ये method उसी जगह पर execute होता है।
यदि आप java library में इस method की definition देखेंगे तो पाएंगे की आप उस code बार बार अपने program में नहीं लिख सकते है। और यदि आप उस code को यूज़ करते है तो आपका पहला hello world program 10 line से ज्यादा का हो जायेगा। 
Methods program में code को reduce करने और computer की memory बचाने का अच्छा माध्यम है। साथ ही methods के प्रयोग से programmers को भी same code बार बार नहीं लिखना पड़ता है। इससे programmers का time बचता है।

Structure of Method Definition 

access_specifier return_type method_name (parameters list)
{
  //  statements to be executed
}
 Syntax  Explanation 
access specifier
Access specifier show करता है की ये method किस तरह का है। जैसे की private, public या protected आदि।     
return type
Method का execution होने के बाद method result के रूप में किस तरह की value return करेगा ये आप यँहा define करते है। यदि आपका method कोई value return नहीं करता है तो आप यँहा पर void लिखते है।    
method name
ये method का नाम होता है। ये पुरे program में unique होना चाहिए।    
parameters list
ये वो variables या values होती है जो आप method को call करते समय pass करते है। Method पर operation perform करता है और result return करता है। या फिर अपने execution के लिए इनका उपयोग करता है। जैसे यदि आप कोई addition का method बनाये तो उसमे 2 numbers pass कर सकते है जिनको को add करके method आपको result return करेगा। आप values और variables को comma से separate करते है।       
     

Structure of Method Call 

If in the same class
If in another class
methodName(arg1,arg2….);
objectName.methodName(arg1,arg2);

Example of Java Methods 

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 :  Juli   Age : 23”);
        Separator();
       
System.out.println(“Name :  Sam    Age:24”);
       
Separator();    
       
System.out.println(“Name :  Teri   Age : 23”);
        Separator();
       
System.out.println(“Name :  Brian       Age:24”);
     }

Method Overloading 

जब आप बड़े software’s develop करते है तो ऐसा अक्सर होता है की आपके program में एक सा काम करने वाले कई methods होते है। इन सभी methods का नाम याद रखना और उन्हें यूज़ करना बड़ा मुश्किल हो जाता है। जैसे की आप अलग अलग तरह के data को add करना चाहते है तो उसके लिए आप अलग अलग methods बनाएंगे।
फिर उन methods को यूज़ करने के लिए आपको उनका नाम भी memorize करना होगा। किसी छोटे से program में ऐसा आप आसानी से कर सकते है। लेकिन जब आप कोई बड़ा software develop कर रहे हो तो सभी methods के नाम आप याद नहीं कर सकते है। इस situation में आप method overloading यूज़ कर सकते है।
Method overloading में जो methods एक जैसा task perform कर रहे है उन सबको आप एक ही नाम देते है। जैसे की अलग अलग addition के methods addition ही कर रहे है। लेकिन कोई integer को add कर रहा है तो तो कोई double को add कर रहा है।
इन सबका एक ही नाम दिया जाता है। अब आप पूछ सकते है की यदि एक जैसा नाम रखेंगे तो compiler को पता कैसे चलेगा की आप कोनसा method call करना चाहते है।
इन सभी methods का नाम same होता है। लेकिन इन methods में parameter types और उनकी संख्या अलग अलग होती है। जिससे compiler इन्हे run time पर ये पता चल जाता है की कोनसा method execute करना है। जिस method से arguments match हो जाते है वही method execute हो जाता है।

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));
    }
}
         
ऊपर दिए गए example में 2 sum methods है। एक sum() method integers को add करता है और दूसरा double values को add करता है। Method call के दौरान यदि sum() method में integers pass करते है तो integers वाला sum() method execute होगा। और यदि double values pass करते है तो double वाला sum() method execute होगा।         

 

जब आप किसी class को inherit करते है तो आप उसके सारे methods को access कर सकते है। लेकिन क्या हो यदि किसी method की definition आपकी sub class के according नहीं है। या फिर आप उस method में कुछ change करना चाहते है।
यदि ऐसा है तो आप super class के किसी भी method को subclass में दुबारा define कर सकते है। आप उस method को edit कर सकते है ताकि वो sub class के लिए work कर सके। इसे method overriding कहते है।   
Method overriding में subclass superclass वाला method यूज़ नहीं करना चाहती है और वो खुद उस method को edit करके यूज़ करना चाहती है। ऐसा करके वो super class के method को override करती है।
उदाहरण के लिए यदि एक class है Area और उसमे display() method है। इस class को Circle class inherit कर रही है। Circle class Area class के display method को यूज़ करने की बजाय उस method को Circle class की आवश्यकता के अनुसार edit कर सकती है।
इसी प्रकार यदि इसी Area class को एक rectangle class inherit करती है तो वो भी display() method को खुद की आवश्यकता के अनुसार बदल कर वापस define कर सकती है।
जब आप किसी method को override करते है तो आपको 2 बाते याद रखनी चाहिए।
  • Super class के method का और subclass के method का नाम same होगा। 
  • Subclass की parameter list superclass की parameter list से match होनी चाहिए। 

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();
     }
}

Comments

Popular posts from this blog

    METACHAR ACTER   UNIX  shell  अनेक मेटाचारेक्टर प्रोवाइड करता है जो किसी भी शैल स्कफ्रिप्त में उनका उसे करते टाइम विषेस माइनिंग रखते और जब तक काशी सोवळे नहीं हो जाते तब काट किसी वर्ड का एंडिंग का कारन बनते है।                                                     ex:--     एक directory में फिलो को लिस्टिंग करते टाइम एक सिंगल चैरेक्टर में रखा जाता है और * एक से अधिक वर्ड को मैचेस करता है यह शैल के अधिकांश पत्रों को लिस्ट दी गयी है जिन्हे metacharacter बोला  जाता है.   * ? [ ] " ' \    /    $ : ; ( ) | ^ < >  \.   नई लाइन स्पेस तब   #!/bin/sh  echo hello; word  #!/bin/sh  echo "I have \$1200" 1. <  single quotes : all special charecters between these quotes lose their special.  ex. --...
UNIX in Hindi UNIX एक multi tasking, multi user operating system  है जिसे सन 1969  में AT & T LABS  में बनाया गया था। जिसे AT & T में काम करने वाले computer  scientist  ken Thompson  &   Denis Ritchie and  उनके friends ने मिलकर  बनाया गया था.  ऐसे AT & T   ने अपने उसे के लिए बनाया गया था लेकिन बाद में सन 1970 में इसे commercialize कर दिया गया था।  इस Operating System को ख़ास तौर पर Programmers और developers के लिए बनाया गया था। इसकी बजह यह थी की यह Modular Programming Interface design provide करता था जिसे "Unix Philosophy" भी कहा जाता है। UNIX Operating System को 'C' और Assembly Programming Languages में लिखा गया था। C Programming language को खास तौर पर UNIX बनाने के लिए ही develop किया गया था और इसे भी Denis Ritchie द्वारा ही बनाया गया था। आज भी 'C' Programming Language को बहुत ख़ास माना जाता है क्यूंकि Device Drivers इसी language में ज्यादातर लिखे जाते हैं और साथ ही इसे  Mother...
Addressing modes of microprocessor 8085 The way of specifying data to be operated by an instruction is called addressing Types of addressing modes – In 8085 microprocessor there are 5 types of addressing modes: Immediate Addressing Mode – In immediate addressing mode the source operand is always data. If the data is 8-bit, then the instruction will be of 2 bytes, if the data is of 16-bit then the instruction will be of 3 bytes. Examples: MVI B 45 (move the data 45H immediately to register B) LXI H 3050 (load the H-L pair with the operand 3050H immediately) JMP address (jump to the operand address immediately). Register Addressing Mode – In register addressing mode, the data to be operated is available inside the register(s) and register(s) is(are) operands. Therefore the operation is performed within various registers of the microprocessor. Examples: MOV A, B (move the contents of register B to register A) ADD B (add contents of registers A and B and s...