0% found this document useful (0 votes)
158 views21 pages

Relationships and Dynamic Method Dispatch in Java: Instructor

This document discusses relationships (IS-A and HAS-A) and dynamic method dispatch in Java. It provides examples of IS-A relationships using inheritance and HAS-A relationships using object composition. It also explains dynamic method dispatch, where the version of an overridden method that is executed is determined at runtime based on the actual object type, rather than the reference variable type. An example is provided to illustrate how a superclass reference can refer to subclass objects and invoke the appropriate overridden method.

Uploaded by

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

Relationships and Dynamic Method Dispatch in Java: Instructor

This document discusses relationships (IS-A and HAS-A) and dynamic method dispatch in Java. It provides examples of IS-A relationships using inheritance and HAS-A relationships using object composition. It also explains dynamic method dispatch, where the version of an overridden method that is executed is determined at runtime based on the actual object type, rather than the reference variable type. An example is provided to illustrate how a superclass reference can refer to subclass objects and invoke the appropriate overridden method.

Uploaded by

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

Relationships and Dynamic

Method Dispatch in Java

Instructor Name: Muhammad Fayyaz Awan


Department of Computer Science, COMSATS University Islamabad, Wah Campus, Pakistan
Contents
2

 IS-A and HAS-A relationship


 IS-A and HAS-A relationship (Example)
 Dynamic method dispatch
 Dynamic Method dispatch (Example)
 References
Relationships (IS-A and HAS-A) in Java
3

• Feature of OOP language is code reusability.

• Two ways we can do code reuse


• Implementation of inheritance (IS-A relationship)
• or object composition (HAS-A relationship).

The compiler and Java virtual machine (JVM) will do a lot of work for you
when you use inheritance, you can also get at the functionality of
inheritance when you use composition.
IS-A Relationships in Java
4

In object-oriented programming, the concept of IS-A is a totally based on


Inheritance, which can be of two types Class Inheritance or Interface
Inheritance.

It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is
a Vehicle etc. Inheritance is uni-directional. For example, House is a Building.
But Building is not a House.

It is a key point to note that you can easily identify the IS-A relationship.
Wherever you see an extends keyword or implements keyword in a class
declaration, then this class is said to have IS-A relationship.
HAS-A Relationships in Java
5

Composition (HAS-A) simply mean the use of instance variables


that are references to other objects. For example Honda has
Engine, or House has Bathroom.

In Aggregation, both the entries can survive individually.


In composition, both the entities are dependent on each other.
IS-A and HAS-A Relationships in Java
6

Class Car

IS-A

Class Honda HAS-A Class Engine

Let’s understand these concepts with an example of Car class.


IS-A and HAS-A Relationship (Example Slide-1)
7

package relationships;
class Car
{
// Methods implementation and class/Instance members
private String color;
private int maxSpeed;
public void carInfo()
{ System.out.println("Car Color= "+color + " Max Speed= " + maxSpeed); } public
void setColor(String color)
{ this.color = color; }
public void setMaxSpeed(int maxSpeed)
{ this.maxSpeed = maxSpeed; }

}
IS-A and HAS-A Relationship (Example Slide-2)
8

class Honda extends Car{


{
public void HondaStartDemo()
{
Engine HondaEngine = new Engine();
HondaEngine.start();
}
//Honda extends Car and thus inherits all methods from
} Car (except final and static)

//Honda can also define all its specific functionality


IS-A and HAS-A Relationship (Example Slide-3)
9

package relationships;
public class Engine
{
public void start()
{ System.out.println("Engine Started:"); }
public void stop()
{ System.out.println("Engine Stopped:");}
}
IS-A and HAS-A Relationship (Example Slide-4)
10

package relationships;
public class RelationsDemo
{
public static void main(String[] args)
{
Honda myHonda = new Honda();
myHonda.setColor("RED");
myHonda.setMaxSpeed(180);
myHonda.carInfo();
myHonda.HondaStartDemo();
}
}
Dynamic Method Dispatch (1/3)
11

Prerequisite: Overriding and Inheritance

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 (2/3)
12

Important Points?

• When an overridden method is called through a superclass


reference, Java determines which
version(superclass/subclasses) of that method is to be executed
based upon the type of the object being referred to at the time
the call occurs. Thus, this determination is made at run time.

• So at run-time, it depends on the type of the object being


referred to: not the type of the reference variable
Dynamic Method Dispatch (3/3)
13

A superclass reference variable can refer to a


subclass object. This is also known as upcasting.
Java uses this fact to resolve calls to overridden
methods at run time.

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.
On next slide, an example that illustrates dynamic method dispatch
Dynamic Method Dispatch (Example)
14

class A class C extends A


{ {
    void m1()     // overriding m1()
    {     void m1()
        System.out.println("Inside A's m1 method");     {
    }         System.out.println("Inside C's m1 method");
}     }
  
}
  class B extends A
{
    // overriding m1()
    void m1()
    {
        System.out.println("Inside B's m1 method");
    }
}
Dynamic Method Dispatch (Example)
15

public static void main(String args[])


    {
        // object of type A
        A a = new A(); // now ref refers to a B object
    ref = b;
        // object of type B  // calling B's version of m1()
        B b = new B();   ref.m1();
     
        // object of type C   // now ref refers to a C object
        C c = new C();   ref = c;
    
        // obtain a reference of type A    // calling C's version of m1()
        A ref;    ref.m1();
          
        // ref refers to an A object     }
        ref = a;
  
        // calling A's version of m1()
        ref.m1();
Dynamic Method Dispatch (Example Explanation)
16

The program creates one superclass called A and it’s two subclasses B and C.
These subclasses overrides m1( ) method.
Dynamic Method Dispatch (Example Explanation)
17
Dynamic Method Dispatch (Example Explanation)
18
Dynamic Method Dispatch (Advantages)
19

1.Dynamic method dispatch allow Java to support overriding of methods which is


central for run-time polymorphism.

2.It allows a class to specify methods that will be common to all of its derivatives,
while allowing subclasses to define the specific implementation of some or all of
those methods.

3. It also allow subclasses to add its specific methods subclasses to define the
specific implementation of some.
References
20

• Absolute JAVA Fifth Edition by Walter Savitch https://


www.slideshare.net/AdilAslam4/inheritance-and-its-type-in-ja
va
• https://
www.w3resource.com/java-tutorial/inheritance-composition-
relationship.php
• https://www.geeksforgeeks.org/dynamic-method-dispatch-ru
ntime-polymorphism-java
/
• https://www.geeksforgeeks.org/association-composition-aggr
egation-java
21

THANK YOU

You might also like