Home > Java > javaTutorial > body text

How do Method Overriding and Method Overloading Differ in Java?

Linda Hamilton
Release: 2024-10-27 20:08:30
Original
447 people have browsed it

How do Method Overriding and Method Overloading Differ in Java?

Distinguishing Dynamic and Static Polymorphism in Java

Understanding the difference between dynamic and static polymorphism is crucial in object-oriented programming with Java. This article will provide a simplified explanation and example to elucidate this concept.

Dynamic vs. Static Polymorphism

Polymorphism allows a single method name to have multiple implementations depending on the object type calling it. There are two primary types of polymorphism:

  • Dynamic Polymorphism (Runtime Binding): Method calls are resolved at runtime based on the actual object's class. It occurs with method overriding in subclasses and is also known as late binding.
  • Static Polymorphism (Compile-Time Binding): Method calls are resolved at compile-time based on the declared type of the reference variable. It occurs with method overloading in the same class and is also known as early binding.

Method Overloading

Method overloading is a form of static polymorphism where multiple methods with the same name exist in the same class but differ in their parameters. When calling an overloaded method, Java determines the appropriate method to invoke based on the number and types of arguments passed in.

Code Example (Method Overloading):

<code class="java">class Calculation {
  void sum(int a, int b) { System.out.println(a + b); }
  void sum(int a, int b, int c) { System.out.println(a + b + c); }

  public static void main(String[] args) {
    Calculation obj = new Calculation();
    obj.sum(10, 10, 10); // Output: 30
    obj.sum(20, 20); // Output: 40
  }
}</code>
Copy after login

Method Overriding

Method overriding is a form of dynamic polymorphism where methods with the same name and signature are declared in different classes but share a common parent class. When calling an overridden method, Java determines the method to invoke based on the object's actual class at runtime.

Code Example (Method Overriding):

<code class="java">class Animal {
  public void move() {
    System.out.println("Animals can move");
  }
}

class Dog extends Animal {

  public void move() {
    System.out.println("Dogs can walk and run");
  }
}

public class TestDog {

  public static void main(String[] args) {
    Animal a = new Animal(); // Animal reference and object
    Animal b = new Dog(); // Animal reference but Dog object

    a.move(); // Output: Animals can move

    b.move(); // Output: Dogs can walk and run
  }
}</code>
Copy after login

The above is the detailed content of How do Method Overriding and Method Overloading Differ in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template