Java method overloading and overriding

          

Java method overloading and overriding

<<Previous   Next>>

Define Method overloading. Explain its uses. Provide a code sample to explain the uses of Method overloading

  • Java supports to define two or more methods with same names within a class.
  • All the methods should differ in either by the number or type of parameters.
  • The method System.out.println() receives multiple parameters.
  • Ex : System.out.println(“Welcome to Java Technology”);// parameter as String type
    System.out.println(number);// parameter as integer type
  • These methods are known as overloaded methods and the process is referred as method overloading
  • One way of implementing polymorphism is method overloading
Uses of method overloading:
  • Method overloading when a couple of methods are needed with conceptually similar functionality with different parameters
  • Memory can be saved by implementing method overloading
The following application demonstrates method overloading:

class OverloadDemo
{
         void triangleArea(float base, float height)
         {
               float area;
               area = base * height / 2.0f;
               System.out.println(“Area = “ + Area);
         }

         void triangleArea(float side1, float side2, float side3) 
         {
                  float area,s;
                  s = (side1 + side2 + side3) / 2.0; 
                  area = Math.sqrt(s*(s-side1) * (s-side2) * (s-side3) );
                  System.out.println(“Area = “ + area);
         }
}

class MainOverloadDemo
{
            public static void main(String args[])
            {
                    OverloadDemo ovrldDemo = new OverloadDemo(); 
                    ovrldDemo.triangleArea(20.12,58.36);
                    ovrldDemo triangleArea(63.12,54.26,95.24);
            }
}

Define Method overriding. Explain its uses. Provide a code sample to explain the uses of Method overloading

Method overriding is the process of writing functionality for methods with same signature and return type, both in super class and subclass

The uses of method overriding:
  • Time to invest method signature is reduced
  • Different functionality in both super class and sub class by sharing same signature
  • The functionality can be enhanced
  • The behavior can be replaced in the sub class
code example for method overriding:

class Super
{
     int sum;
     A(int num1, int num2)
     {
             sum = a+b;
     }
     void add()
     {
            System.out.println("Sum : " + sum);
     }
}
class Sub extends Sub
{
       int subSum;
       Sub(int num1, int num2, int num3)
       {
               super(num1, num2);
               subSum = num1+num2+num3;
       }
       void add()
       {
               super.add();
               System.out.println("Sum of 3 nos : " +subSum);
       }
}

Difference between overloading and overriding  

Overloading:
  • Two methods have same name with different type or number of parameters
  • A relationship between methods is available in the same class
  • Inheritance does not blocked by method overloading
  • One method can overload unlimited number of times
  • Only method name can be reused
Overriding:
  • A method of super class is redefined in the sub class
  • A relationship between methods is available in the super class and its sub class
  • Method overriding blocks the inheritance
  • Method overriding can be done only once per method in the sub class
  • The complete method signature can be reused
<<Previous   Next>>
Overridden methods in Java

Overridden methods of super class in subclass allow a class to inherit and behave close enough. To avoid naming conflict and signature and type and number of parameters..............

Define overriding. Explain it with an example.

Overriding is done in a child class for a method that is written in the parent class. To override a method a new method is defined in the child class with exactly the same signature............

Define a final class in java. Explain the purpose of final class.

A class declared as final cannot be extended further by any other class. The best example of such a class is the String class................

What is co-variant return type in java? Provide an example and its purpose

A covariant return type allows to override a super class method that returns a type that sub class type of super class method’s return type. It is to minimize up casting and down casting.............

Write your comment - Share Knowledge and Experience


 

 
 
Home | Want a Job? Submit Key Skills | Employer login | My key skills | About us | Sitemap | Contact us