Recent Posts

header ads

Method in java with examples - Method Overloading and Overriding । KpTechSolution

➧Methods in Java - Introduction :

We also call "Methods" as "Functions" in Java. Many times when we are developing a big software So we do not have to write any code or any information repeatedly, so we all need Methods.
Methods in java with examples - Method Overloading and Overriding - हिन्दीं में पढ़ें ।
Methods in Java Programming Language
The code written inside the Methods can be implemented by the Programmer, where it wants to implement, only the class in which the method is made, has to make an object of that class and call its method. By calling that method, we can do its property wherever we want to implement it. Using the method, code written inside it does not have to be repeated anywhere.

"Methods" is a block inside which Programmer writes some Statements. These statements are easily used by calling their methods where necessary. Using Methods reduces the size of our program and becomes in Program Short. Time also gets reduce.

For "example" we can take the println ("...") function. The println () function is a Pre-defined Function that is placed in the java library in the "java.io" package, in the PrintStream class. Whenever we have to print any statements, we call it Simple bit println ("...") and use it. println ("...") would have been defined somewhere within a class anywhere in Java's Library, so it is Responsible to print a Statement.

Source Code for Pre-defined Methods :
  1. class Example
    {
    String a = "Shivam ";
    public static void main (String args [ ] )
    {
    System.out.println ( a ); //println() is Pre-defined 
    }
    }
OUTPUT: Shivam

Syntax :
Access_specifier  Return_types  Method_nameParameters )
 {  // Statements  }

    Access_specifier :
    In the Java Programming Language, Access_specifier defines how the method is accessible in any other class. Such as Public, Private, Protected If public, then everyone will use its property. Private will not be able to use any of its properties and if it is protected it will be able to access both of it itself and its sub-class only.

    Return_type :
    Return type means that our method will return the type of value. Such as int, float, string and if "void" is typed it means that our method will not return any value.

    Method_name :
    In Method_name we write the name of our method, whatever we have to type the name of our method, this name can be anything. It depends on the programmers.

    Parameters :
    Parameters are passed here and Parameters are also passed here. It depends on the programmers. Because Methods are used in two types, one is Parameterized Methods and the second Non-Parameterized Methods.


    For ex :
    1. public int display ( int x, int y )
       {
        }

    Syntax for Method Calling :


     1. In same class -> Method_name( args1,args2,….);

     2. In another class -> Object_name.Method_name(args1,args2,….)


    Example for main Method :
    1. class Example
      {
      public static void main (String args []) //This is "main( )".
      {
      System.out.println(“ I am main method “ );
      Example ob = new Example ();
      }
      }

    Read Also :

    Methods with No Arguments :

    While making these types of methods, some arguments can also be passed in it. This argument holds the Value given at the time of calling this method by creating an object of the class of this method. For example, let's understand this through a Java Source Code -


    Source Code :
    1. class Example
      {
      public int display ( )  // Method
      {
      int a = 10, b = 20, result ;
      result = a + b ;
      System.out.println(“a + b = “+result);
      }
      public static void main ( String args [ ] )
      {
      Example obj = new Example ( ) ;
      obj.display ( ) ;
      }
      }
    OUTPUT : a + b = 30

    Methods with Arguments :

    Source Code :
    1. class Example
      {
      int x, y, result ;
      public int display ( int n, int m )  // Method
      {
      x = n;
      y = m ;
      result = x + y ;
      System.out.println(“a + b = “+result);
      }
      public static void main ( String args [ ] )
      {
      Example obj = new Example ( ) ;
      obj.display ( 10, 20 ) ;
      }
      }
    OUTPUT :  a + b = 30

    Static Methods :

    To create Static Method we use "static" keyword in that method. By using the "static" keyword in the method, we can call that method without having the object of the class of that method, for example we understand the multiplication of two numbers with the Java Source Code.

    Source Code :
    1. class Example
      {
      public static ind show ( int a, int b )
      {
      result = a * b ;
      return result ;
      }
      public static void main (String args [ ] )
      {
      int x = show ( 10, 20 ) ;
      System.out.println ( x ) ;
      }
      }
    OUTPUT : 200


    Also Read :

    Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.

    Thank You !


    Note - For More Core Java & Advance Java Tutorials, Concepts & Programs Are Available in www.kptechsolution.com  

    Post a Comment

    0 Comments