Programs on Methods

Methods

Methods 
             Methods are used to perform a logical operations and can be used for reusability .

Types of methods :
  1. Default method.
  2. Parameterized Method. 
  3. Static Method.
  4. Final Method.

1.Default method:

Programs Using Default Method


class DefaultMethod 
{
int a=10;
int b=20;

void defaultAdd()
{
System.out.println("addition of two numbers : "+(a+b));
}
public static void main(String[] args) 
{
DefaultMethod dm=new DefaultMethod();

dm.defaultAdd();
}

}

Save

DefaultMethod.java

Compile 

javac DefaultMethod.java

Run

java DefaultMethod

Output:

addition of two numbers :30



Programs Using Parameterized Method



import java.util.*;



class ParameterizedMethod 

{

void parameterizedAdd(int a,int b)

{

System.out.println("adiition of two numbers : "+(a+b));

}

public static void main(String[] args) 

{

Scanner sc=new Scanner(System.in);


System.out.println("enter two numbers : ");
int a=sc.nextInt();
int b=sc.nextInt();

ParameterizedMethod pm=new ParameterizedMethod();

pm.parameterizedAdd(a,b);
}
}

Save


ParameterizedMethod.java

Compile 

javac ParameterizedMethod.java

Run

java ParameterizedMethod

Input:

Enter two numbers:
10
20

Output:

addition of two numbers :30.

Programs Using Static Method.


import java.util.*;

class StaticMethod 
{
 static void staticAdd(int a,int b)
 {
  System.out.println("adiition of two numbers : "+(a+b));
 }
 public static void main(String[] args) 
 {
  Scanner sc=new Scanner(System.in);

  System.out.println("enter two numbers : ");
  
  int a=sc.nextInt();
  int b=sc.nextInt();

  StaticMethod sm=new StaticMethod();

  sm.staticAdd(a,b);

StaticMethod.staticAdd(a,b);
 }
}

Save:

StaticMethod .java

Compile:

javac StaticMethod .java

Run:

java StaticMethod .

Input:

enter two numbers:
10
20

Output:

addition of two numbers: 30.      //using object name.
addition of two numbers: 30.    //using class name.

Note:

A static Method can even called using class name .
Static method allows only static variables.

Programs Using Final Method.



import java.util.*;

class FinalMethod 
{
 final void finalAdd(int a,int b)
 {
  System.out.println("adiition of two numbers : "+(a+b));
 }
 public static void main(String[] args) 
 {
  Scanner sc=new Scanner(System.in);

  System.out.println("enter two numbers : ");
  
  int a=sc.nextInt();
  int b=sc.nextInt();
  
  FinalMethod fm=new FinalMethod();

  fm.finalAdd(a,b);

 }
}

Save:

FinalMethod.java

Compile:

javac FinalMethod.java

Run:

java FinalMethod

Input:

enter two values:
10
20

Output:

addition of two numbers:30.

Note:

A final method can be declared using a final keyword. 
A final Method can be inherited from super class to sub class but cant be overridden. 
A final method can not be called using class Name.


Programs Using Method Overloading.


import java.util.*;

class MethodOverLoading 
{
 void OverAdd(int a,int b)
 {
  System.out.println("adiition of two numbers : "+(a+b));
 }

 void OverAdd(int a,int b,int c)
 {
  System.out.println("adiition of three numbers: "+(a+b+c));
 }

 public static void main(String[] args) 
 {
  Scanner sc=new Scanner(System.in);

  MethodOverLoading mol=new MethodOverLoading();

  System.out.println("enter two numbers : ");
  
  int a=sc.nextInt();
  int b=sc.nextInt();
  
  System.out.println("enter three numbers : ");
  
  int c=sc.nextInt();
  int d=sc.nextInt();
  int e=sc.nextInt();

  mol.OverAdd(a,b);
  mol.OverAdd(c,d,e);     //overloaded method.

 }
}

Save:

MethodOverLoading.java

Compile:

javac MethodOverLoading.java

Run:

java MethodOverLoading

Input:

enter two numbers:
10
20
enter three numbers:
10
20
30

Output:

additon for two numbers: 30
addition for three numbers: 60.

Note:

Method Overloading means same method with different parameters or of different type of parameters or different order of parameters.
Method Overloading can be done only in the same class.             

Programs Using Method Over-riding.



import java.util.*;

class MethodOverRiding 

{

void methodOver()

{
System.out.println("institute for all technical languages : Void Main Technologies");
}
}
class MethodOverRidingTest extends MethodOverRiding
{
void methodOver()
{
System.out.println("Courses Provided are c,c++,java,frameworks and many more technologies in void main technologies");
}
public static void main(String args[])
{
MethodOverRiding mr=new MethodOverRiding();

mr.methodOver();
}
}


Save:

MethodOverRidingTest.java

Compile:

javac MethodOverRidingTest.java

Run:

java MethodOverRidingTest

Output:


institute for all technical languages : void Main Technologies

Note: 

Two methods of same type in both super and sub classes is called as method Overriding.
Final methods cant be overridden. 




Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Inner Classes