0% found this document useful (0 votes)
2 views

java_research

Method overriding occurs when a subclass method has the same name and signature as a superclass method, allowing the subclass method to be called instead. The superclass method can still be accessed using 'super'. Dynamic method dispatch allows Java to resolve which overridden method to execute at runtime based on the actual object type, enabling runtime polymorphism.

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java_research

Method overriding occurs when a subclass method has the same name and signature as a superclass method, allowing the subclass method to be called instead. The superclass method can still be accessed using 'super'. Dynamic method dispatch allows Java to resolve which overridden method to execute at runtime based on the actual object type, enabling runtime polymorphism.

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Method Overriding

In a class hierarchy, when a method in a subclass has the same name and
type signature as a method in its superclass, then the method in the
subclass is said to override the method in the superclass. When an
overridden method is called through its subclass, it will always refer to the
version of that method defined by the subclass. The version of the method
defined by the superclass will be hidden. Consider the following:
The output produced by this program is shown here:
k: 3

When show( ) is invoked on an object of type B, the version of show( )


defined within B is used. That is, the version of show( ) inside B overrides
the version declared in A.
If you wish to access the superclass version of an overridden method,
you can do so by using super. For example, in this version of B, the
superclass version of show( ) is invoked within the subclass’ version. This
allows all instance variables to be displayed.

If you substitute this version of A into the previous program, you will
see the following output:
i and j: 1 2
k: 3

Here, super.show( ) calls the superclass version of show( ).


Method overriding occurs only when the names and the type signatures
of the two methods are identical. If they are not, then the two methods are
simply overloaded. For example, consider this modified version of the
preceding example:
The output produced by this program is shown here:
This is k: 3
i and j: 1 2

The version of show( ) in B takes a string parameter. This makes its


type signature different from the one in A, which takes no parameters.
Therefore, no overriding (or name hiding) takes place. Instead, the version
of show( ) in B simply overloads the version of show( ) in A.

Dynamic Method Dispatch


While the examples in the preceding section demonstrate the mechanics of
method overriding, they do not show its power. Indeed, if there were
nothing more to method overriding than a name space convention, then it
would be, at best, an interesting curiosity, but of little real value. However,
this is not the case. Method overriding forms the basis for one of Java’s
most powerful concepts: dynamic method dispatch. Dynamic method
dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time. Dynamic method dispatch
is important because this is how Java implements run-time polymorphism.
Let’s begin by restating an important principle: a superclass reference
variable can refer to a subclass object. Java uses this fact to resolve calls
to overridden methods at run time. Here is how. When an overridden
method is called through a superclass reference, Java determines which
version of that method to execute based upon the type of the object being
referred to at the time the call occurs. Thus, this determination is made at
run time. When different types of objects are referred to, different
versions of an overridden method will be called. In other words, it is the
type of the object being referred to (not the type of the reference variable)
that determines which version of an overridden method will be executed.
Therefore, if a superclass contains a method that is overridden by a
subclass, then when different types of objects are referred to through a
superclass reference variable, different versions of the method are
executed.
Here is an example that illustrates dynamic method dispatch:

You might also like