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()
{ [Link]("Car Color= "+color + " Max Speed= " + maxSpeed); } public
void setColor(String color)
{ [Link] = color; }
public void setMaxSpeed(int maxSpeed)
{ [Link] = maxSpeed; }
}
IS-A and HAS-A Relationship (Example Slide-2)
8
class Honda extends Car{
{
public void HondaStartDemo()
{
Engine HondaEngine = new Engine();
[Link]();
}
//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()
{ [Link]("Engine Started:"); }
public void stop()
{ [Link]("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();
[Link]("RED");
[Link](180);
[Link]();
[Link]();
}
}
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()
[Link]("Inside A's m1 method"); {
} [Link]("Inside C's m1 method");
} }
}
class B extends A
{
// overriding m1()
void m1()
{
[Link]("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
[Link] method dispatch allow Java to support overriding of methods which is
central for run-time polymorphism.
[Link] 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://
[Link]/AdilAslam4/inheritance-and-its-type-in-ja
va
• https://
[Link]/java-tutorial/inheritance-composition-
[Link]
• [Link]
ntime-polymorphism-java
/
• [Link]
egation-java
21
THANK YOU