OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 16 : Method Overriding in Java

 

Unit - 2

Topic - 16: Method Overriding in Java

Definition

In a class hierarchy, when a method in a subclass has the same name and same type signature as a method in its superclass, the method in the subclass overrides the one in the superclass.


Key Points

  • When an overridden method is called from a subclass object, it will always execute the subclass version.

  • The superclass version is hidden.

  • First, Java checks if the method exists in the superclass.

    • If yes, it executes the subclass version.

    • If no, it throws a "method not found" error.

  • Methods with different signatures are overloaded, not overridden.


Example Program

// Method Overriding Example class A { int i, j; A(int a, int b) { i = a; j = b; } // Display i and j void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); // Call superclass constructor k = c; } // Overriding show() from class A void show() { System.out.println("k: " + k); } } class OverrideDemo { public static void main(String args[]) { B subOb = new B(1, 2, 3); subOb.show(); // Calls subclass version } }

Output

k: 3

Dynamic Method Dispatch in Java

Definition

Dynamic Method Dispatch is a mechanism by which a call to an overridden method is resolved at runtime rather than compile time.
It enables runtime polymorphism in Java.


Before You Begin

We must understand the concept of superclass reference and subclass object:

  • A reference variable of a superclass can refer to an object of its subclass.

  • Which version of the overridden method gets called is decided at runtime, based on the actual object being referred to.


Example Program

// Dynamic Method Dispatch Example class A { void callme() { System.out.println("Inside A's callme method"); } } class B extends A { // Override callme() void callme() { System.out.println("Inside B's callme method"); } } class C extends A { // Override callme() void callme() { System.out.println("Inside C's callme method"); } } class Dispatch { public static void main(String args[]) { A a = new A(); // Object of type A B b = new B(); // Object of type B C c = new C(); // Object of type C A r; // Reference of type A r = a; // r refers to an A object r.callme(); // Calls A's version r = b; // r refers to a B object r.callme(); // Calls B's version r = c; // r refers to a C object r.callme(); // Calls C's version } }

Output

Inside A's callme method Inside B's callme method Inside C's callme method

Explanation

  • The reference r is of type A, but it can refer to objects of A, B, or C.

  • The actual method execution depends on the object type that r refers to at runtime.

  • This behavior is the foundation of polymorphism in Java.

Comments

Popular posts from this blog

Career Guide - B.Tech Students

Artificial Intelligence - UNIT - 1 Topic - 1 : Introduction to AI (Artificial Intelligence)

Financial Aid for Students: Scholarships from Government, NGOs & Companies