Monday, August 12, 2019

OOPS –Method Overloading & Method Overriding


Method Overloading & Method Overriding 



Overriding and Overloading are two very important concepts in Java.

Overloading:
Overloading occurs when two or more methods in one class have the same method name but different parameters.
·        Method overloading is also called as compile time polymorphism
·        Method overloading performed within class
·        Method overloading is used to increase the readability of the program.

Example:
An Example of Overloading
class Dog
   {
       public void bark()
        {
        System.out.println("woof ");
       }
   

    //overloading method
    public void bark(int num)
     {
       System.out.println("woof ");
     }
}


In this overloading example, the two bark method can be invoked by using different parameters. Compiler know they are different because they have different method signature (method name and method parameter list).

Overriding:
Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

·        It allows to different implementation in child class with same method name.
·        Method overriding is also called as run time polymorphism
·        Method overriding occurs in two classes that have a IS-A (inheritance) relationship
·        Method overriding is used to provide the specific implementation of the method that is already provided by its super class.

Example:
Here is an example of overriding:
class Dog
  {
      public void bark()
      {
         System.out.println("woof ");
      }
 }
class Hound extends Dog
   {
       public void sniff()
      {
         System.out.println("sniff ");
      }

    public void bark()
     {
        System.out.println("bowl");
    }
}

public class OverridingTest
{
    public static void main(String [] args)
  {
        Dog dog = new Hound();
        dog.bark();
    }
}



Output:
bowl
In the example above, the dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned to dog. The JVM knows that dog is referring to the object of Hound, so it calls the bark() method of Hound. This is called Dynamic Polymorphism.

Can we override static method?
No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.

Why can we not override static method?
It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.

Can we override java main method?
No, because the main method is a static method.

No comments:

Post a Comment

If any suggestions or issue, please provide