Difference between method Overloading and Overriding in OOPs

Earlier we discussed about the basics of OOPs and its features. Also we saw the concepts like Abstraction, Encapsulation, Inheritance and Polymorphism. Today we will discuss about a very common interview question and an often misunderstood concept of difference between method overloading and overriding in Object Oriented Programming and polymorphism which can be achieved by it.

Method Overloading

  • To call an overloaded method in Java, it is must have the same name as that of the function.
  • Overloaded methods may have different return types and same or different the return type which will uniquely identify the function.
  • When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. That depends on the type and number of arguments.
  • It allows the user to achieve compile time polymorphism.
  • An overloaded method can throw different exceptions.
  • It can have different access modifiers (It can be public, private or protected).

Rules for Method Overloading

  1. Overloading can be applied in the same class or in the sub-class.
  2. Constructor in Java can also be overloaded.
  3. Overloaded methods must have a different argument list.
  4. The parameters may differ in their type or number, or in both.
  5. They may have the same or different return types.
  6. It is also known as compile-time polymorphism.
Example of Method Overloading
Class Shape
   void area (double radius)
      System.out.println(“Area of circle”);
   void area( double length, double breadth)
       System.out.println(“Area of Rectangle”);
  void area ( double side)
      System.out.println(“Area of Square”);

Method Overriding

Child class and the base class have the same function. In this case the child class overrides the parent class method without changing the functionality of the base class.

Rules for Method Overriding:

  1. Function overloading is applied to inherited methods.
  2. The decisions of which overridden method will take place at the runtime not compile time.
  3. Overriding method can have different return type.
  4. Overriding method must not have more restrictive access modifier(the overridden method should have higher access modifier than overriding method)
  5. Abstract methods must be overridden
  6. Static and final methods cannot be overridden
  7. Constructors cannot be overridden. Because for a particular class its constructors are unique.
  8. It is also known as Runtime polymorphism.
Example of Method Overriding

class House {
    String Street, City, OwnerName;
    long HouseNo;
    House(String Street, String City, String OwnerName,long HouseNo)
        this.Street = Street;
        this.City = City;
        this.OwnerName = OwnerName;
        this.HouseNo = HouseNo;
    void displayHouseDetails()
          System.out.println("House No :" +HouseNo);
          System.out.println("OwnerName :" +OwnerName);
          System.out.println("Street Name :" +Street);
          System.out.println("City Name :" +City);
 class RentedHouse extends House
     double rent;
      RentedHouse(String Street, String City, String OwnerName,long HouseNo,double rent)
       super(Street, City,OwnerName,HouseNo);
       this.rent = rent;
//method overriding from super class to sub class
 void displayHouseDetails()
         System.out.println("Rent: " +rent);
In this we have base class as House which hold properties like House detail (Size of the house, location of the house, House No, owner of the house).These features can be used by RentedHouse subclass which will inherit these features and also add some other functionalities which will be specific to rentedHouse.

Copyright © ianswer4u.com

0 Reactions:

Post a Comment