0% found this document useful (0 votes)
127 views11 pages

Lecture 14 - Dynamic Method Dispatch

Method overriding allows a subclass to provide a specific implementation of a method that is already provided by its superclass. When an overridden method is called from the subclass, it will always refer to the version defined in the subclass, hiding the superclass version. Dynamic method dispatch determines which version of an overridden method to call at runtime based on the actual object type.

Uploaded by

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

Lecture 14 - Dynamic Method Dispatch

Method overriding allows a subclass to provide a specific implementation of a method that is already provided by its superclass. When an overridden method is called from the subclass, it will always refer to the version defined in the subclass, hiding the superclass version. Dynamic method dispatch determines which version of an overridden method to call at runtime based on the actual object type.

Uploaded by

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

INHERITANCE

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 from within a


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.

• If you wish to access the superclass version of an


overridden function, you can do so by using super.
Real example of Java Method Overriding
• Consider a scenario, Bank is a class that provides
functionality to get rate of interest. But, rate of
interest varies according to banks. For example,
SBI, ICICI and AXIS banks could provide 8%, 7% and
9% rate of interest.
Method Overriding...
class A {
int i, j;
A(int a, int b) { i = a; j = b; }
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); k = c; }
void show() { System.out.println("k: " + k); } }
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); }}

O/P: k: 3 // For accessing superclass show(), add super.show() also


// inside the definition of show() in class B.
void show() { super.show(); System.out.println("k: " + k); } }
Can we override static methods in java
• NO. Its not possible to override static methods because static means
class level so static methods not involve in inheritance.

class SuperClassDemo
{
 
public static void staticMethod()
{
 
System.out.println("SuperClassDemo staticMethod called");
 
}
 
}
class SubClassDemo extends SuperClassDemo
{
 
public static void staticMethod()

System.out.println("SubClassDemo staticMethod called"); 
}
public static void main(String []args){  
  SuperClassDemo superObj= new SuperClassDemo();
  SuperClassDemo  superobj1= new  SubClassDem(); 
  SubClassDemo subObj= new  SubClassDem(); 

  // Here is no need to create object to call a static method.


  superObj.staticMethod();
  superObj1.staticMethod();
  subObj.staticMethod(); 
}
}
Can we change accessibility modifier in subclass overridden method?

Yes we can change accessibility modifier in subclass


overridden method but should increase the accessibility if we
decrease compiler will throw an error message.
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.

• Method overriding actually gives power to


Dynamic method dispatching by providing new
definition for overridden functions in each
subclass. This is polymorphism ‘one interface
provided by superclass and multiple methods’.
Dynamic Method Dispatch...
Ex. class A {
void callme() { System.out.println("Inside A's callme method"); } }
class B extends A {
void callme() { System.out.println("Inside B's callme method"); } }
class C extends A {
void callme() { System.out.println("Inside C's callme method"); } }

class Dispatch {
public static void main(String args[]) {
A a = new A(); B b = new B(); C c = new C();
A r; r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object
r.callme(); // calls B's version of callme
r = c; // r refers to a C object
r.callme(); } } // calls C's version of callme
Method Overriding...
Ex. class Figure {
double dim1; double dim2;
Figure(double a, double b) { dim1 = a; dim2 = b; }
double area() {
System.out.println("Area for Figure is undefined."); return 0;
} }
class Rectangle extends Figure {
Rectangle(double a, double b) { super(a, b); }
double area() { System.out.println("Inside Area for Rectangle.");
return dim1 * dim2; }}
class Triangle extends Figure {
Triangle(double a, double b) { super(a, b); }
double area() { System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2; } }
class FindAreas {
public static void main(String args[]) {
Method Overriding...
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area()); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy