Java - method overloading and method overriding,final

                                  Java
What is method overloading?
a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java.

Eg:
class Dsp
{
    public void disp(char a)
    {
         System.out.println(a);
    }
    public void disp(char c, int b)
    {
         System.out.println(c + " "+b);
    }
}
class Sample
{
   public static void main(String args[])
   {
       Dsp d1 = new Dsp();
       d1.disp('x');
       d1.disp('z',10);
   }
}

What is method overriding?
Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.

Eg:
Class test
{
Int a=20;
}
Class Dsp extends test{
{
Int a=50;
}
Public void disp()
{
System.out.println("base class"+super.a);
System.out.println("child"+a):
}
Public static void main(String[]qrgs){
Dsp d1=new Dsp();
D1.disp();
}
}

Difference between Method overloading and method overriding:

Final:
the final keyword is used in several contexts to define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.

Eg:

class Dsp
{

   public static void main(String args[])

  {

    final int i = 30;
  {
    i = 60;
  System.out.println("the i value is "+i);
  }

  }

}

Comments

Popular posts from this blog

Soft Skills

Webnode

Angilar Js