Difference between method overloading and method overriding in java

Overloading Main Method - Demo

Introduction to Method Overloading and Method Overriding in Java

Method overloading in Java

Method overloading in Java is a feature that allows a class to have multiple methods with the same name, but with different parameters. This can be useful for performing the same operation on different types of data, or for providing different levels of functionality for the same operation.

For example, the following code shows a simple example of method overloading:

public class MyClass {
  public void add(int a, int b) {
    System.out.println(a + b);
  }

  public void add(double a, double b) {
    System.out.println(a + b);
  }
}

In this example, the `MyClass` class has two `add()` methods, both of which add two numbers together. However, the first `add()` method accepts two integer parameters, while the second `add()` method accepts two double parameters.

To call the `add()` method, you simply need to specify the appropriate parameters. For example, the following code would call the first `add()` method:

MyClass myClass = new MyClass();
myClass.add(1, 2);

This code would print the following output to the console:

" 3 "

The following code would call the second `add()` method:

myClass.add(1.5, 2.5);

This code would print the following output to the console:

" 4.0 "

Method overloading can be a very useful feature, but it is important to use it carefully. If you are not careful, you can accidentally create methods that are difficult to understand and maintain.

Here are some tips for using method overloading effectively:

  • Make sure that the different overloaded methods have different parameter lists. This will help to prevent the compiler from becoming confused and giving you errors.
  • Choose names for your overloaded methods that are descriptive of what they do. This will make your code more readable and maintainable.
  • Avoid overloading methods with too many parameters. This can make your code difficult to read and understand.
  • Use documentation to explain what each overloaded method does. This will help other programmers to understand your code more easily.

If you are working with Java, I encourage you to learn more about method overloading and how to use it effectively. It is a powerful feature that can help you write more robust and efficient code.

Method overriding in Java

Method overriding is a feature in Java that allows a subclass to provide its own implementation of a method that is already defined in its superclass. This can be useful for extending the behavior of the method in the superclass, or for providing a more specialized implementation of the method for the subclass.

For example, the following code shows a simple example of method overriding:

public class Animal {
  public void eat() {
    System.out.println("The animal is eating.");
  }
}

public class Dog extends Animal {
  @Override
  public void eat() {
    System.out.println("The dog is eating.");
  }
}

In this example, the `Dog` class overrides the `eat()` method that is defined in the `Animal` class. This means that when you call the `eat()` method on a `Dog` object, the `eat()` method that is defined in the `Dog` class will be called instead of the `eat()` method that is defined in the `Animal` class.

Method overriding can be a very useful feature, but it is important to use it carefully. If you are not careful, you can accidentally override methods in a way that breaks your code.

Here are some tips for using method overriding effectively:

  • Make sure that the overriding method has the same signature as the overridden method. This means that the method must have the same name, return type, and parameter list as the overridden method.
  • Use the `@Override` annotation to indicate that a method is overriding a method in the superclass. This will help to prevent errors and make your code more readable.
  • Be careful not to break the behavior of the overridden method. If you are not careful, you could accidentally introduce a bug into your code.

If you are working with Java, I encourage you to learn more about method overriding and how to use it effectively. It is a powerful feature that can help you write more robust and efficient code.

Here are some of the benefits of using method overriding:

  • It allows you to extend the behavior of methods in the superclass. This can be useful for adding new functionality or customizing the behavior of existing methods.
  • It allows you to provide more specialized implementations of methods for subclasses. This can be useful for making your code more efficient and reusable.
  • It can help to improve the readability and maintainability of your code. By overriding methods in the superclass, you can make it clear what methods are being overridden and what new functionality is being added.

Overall, method overriding is a powerful and flexible feature in Java that can be used to write more robust, efficient, and reusable code.

Exercises

Demo Code for Method Overloading and Method Overriding in Java


package javalabdemo;
public class javalabclass 
{
  public static void main(String args[])
    {
    professor satish = new professor("satish","vellore","123","116");
    satish.display_professor();
    satish.display_faculty();
    satish.display_details("hello");
    }
}

class person
{
  protected String name;
  protected String address;
  
  public person(String name,String address) {
    System.out.println("super class constructor called");
    this.name=name;
    this.address=address;
  }
  
  public void display_details() {
    System.out.println(this.name+this.address);
  }
  //overloading display_details() method in the person class.
  public void display_details(String input)
  {
    System.out.println(input+"this is overloading");
  }
}	
  
class faculty extends person
{
  protected String empid;
  
  public faculty(String name,String address,String empid) {
    super(name,address);
    System.out.println("subclass constructor called");
    this.empid=empid;
  }
  
  public void display_faculty()
  {
    System.out.println(this.empid);
  }
  
}

class professor extends faculty
{
  protected String cabinno;
  protected String name;
  
  public professor(String name,String address,String empid, String cabinno) {
    super(name,address,empid);
    this.cabinno=cabinno;
    this.name="test";
  }
  
  public void display_professor() {
    //super used to call super class variables
    System.out.println(this.name+super.name+super.address+super.empid+this.cabinno);
  }
  
  //overriding display faculty method in subclass
  public void display_faculty()
  {
    System.out.println("this is the overridden method");
  }
} 

Code with Static methods in parent class and child class.


  package javalabdemo;
  public class javalabclass 
  {
    public static void main(String args[])
      {
      
      faculty satish = new faculty("satish","vellore","123");
      faculty.display_hello();
      }
  }
  
  class person
  {
    protected String name;
    protected String address;
    
    public person(String name,String address) {
      System.out.println("super class constructor called");
      this.name=name;
      this.address=address;
    }
    
    public void display_details() {
      System.out.println(this.name+this.address);
    }
    
    public static void display_hello()
    {
      System.out.println("hello from static method");
    }
  }	
    
  class faculty extends person
  {
    protected String empid;
    
    public faculty(String name,String address,String empid) {
      super(name,address);
      this.empid=empid;
    }
    
    public void display_faculty()
    {
      System.out.println(this.empid);
    }
    //overriding a static method in the super class in the subclass 
    //with a static method in the subclass
    //Note:  A static method in the super class cannot be overridden
    // by a non static method in the sub class.
    public static void display_hello()
    {
      System.out.println("Hello from static method in the child class");
    }
  }