0% found this document useful (0 votes)
5 views2 pages

Dynamic Method Dispatch

Dmd

Uploaded by

hasibshahriar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Dynamic Method Dispatch

Dmd

Uploaded by

hasibshahriar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Superclass Variables Referencing Subclass Objects

In Java, when a class inherits from another class (subclass inherits from superclass), a superclass
variable can hold a reference to an object of its subclass. This is because a subclass object is also
considered an object of its superclass [This is the reason behind restrictions of constructor]. It
inherits all the members (variables and methods) defined in the superclass, along with any new
members it defines itself.

Here's an example:

Java
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}

class Dog extends Animal {


@Override
public void makeSound() {
System.out.println("Woof!");
}
}

public class Main {


public static void main(String[] args) {
Animal myAnimal = new Dog(); // Superclass reference to subclass object
myAnimal.makeSound(); // Calls the overridden method in Dog (Woof!)
}
}

In this example:

 Animal is the superclass.


 Dog is the subclass that inherits from Animal.
 myAnimal is a variable of type Animal, but it references a Dog object. This is allowed
because Dog is also an Animal.

Reference Variables

A reference variable is a variable that stores a memory address. It acts like a pointer, indicating
where the actual object resides in memory. When you create an object using new, the object is
allocated memory, and a reference variable holds the address of that memory location.

In the example above:

 myAnimal is a reference variable of type Animal.


 It doesn't hold the actual Dog object itself, but rather the memory address where the Dog
object is located.
Dynamic Method Dispatch (Polymorphism)

When you call a method on a superclass reference variable that points to a subclass object,
dynamic method dispatch comes into play. This means that the actual method that gets executed
is determined at runtime based on the object's actual type (subclass). In the example:

 You call myAnimal.makeSound().


 Since myAnimal references a Dog object, the makeSound() method defined in the Dog
class is invoked (which prints "Woof!").

This is a powerful feature of object-oriented programming known as polymorphism. It allows


you to write code that works with objects of different subclasses without explicitly checking their
types.

Key Points:

 Superclass variables can hold references to subclass objects.


 Reference variables store memory addresses of objects.
 Dynamic method dispatch determines the method to be called at runtime based on the
object's type.
 Polymorphism allows flexible code that works with various subclasses.

You might also like