Check out solved MCS 024 assignment question 4 of IGNOU MCA second semester. What is polymorphism? Explain its advantages with the help of a program. What is constructor overloading? Explain the advantage of constructor overloading with the help of an exa
Objective
You will learn about the following in this guide / study material.
Fully solved question 4 of IGNOU MCS 024 assignment 2018-19.
IGNOU MCS 024 assignment 2018-19 Question 4 solution
Total three questions are there in IGNOU MCS 024 assignment question no.4. Let us see the answers for the given questions one by one.
Question 4(a) - What is polymorphism? Explain its advantages with the help of a program. (4 Marks)
Polymorphism
Polymorphism is the capability of a method to do different things based on the object through which it is invoked or object it is acting upon. For example method
find _area will work definitely for Circle object and Triangle object In Java, the type of actual object always determines method calls; object reference type doesn’t play any role in it. You have already used two types of polymorphism (overloading and
overriding) in the previous unit and in the current unit of this block. Now we will look at the third: dynamic method binding. Java uses Dynamic Method Dispatch
mechanism to decide at run time which overridden function will be invoked. Dynamic Method Dispatch mechanism is important because it is used to implement runtime polymorphism in Java. Java uses the principle: “a super class object can refer to a subclass object” to resolve calls to overridden methods at run time.
If a superclass has a method that is overridden by its subclasses, then the different versions of the overridden methods are invoked or executed with the help of a superclass reference variable.
Assume that three subclasses (Cricket_Player Hockey_Player and Football_Player) that derive from Player abstract class are defined with each subclass having its own Play() method.
abstract class Player // class is abstract
{
private String name;
public Player(String nm)
{
name=nm;
}
public String getName() // regular method
{
return (name);
}
public abstract void Play();
// abstract method: no implementation
}
class Cricket_Player extends Player
{
Cricket_Player( String var)
{
}
public void Play()
{
System.out.println("Play Cricket:"+getName());
}
}
class Hockey_Player extends Player
{
Hockey_Player( String var)
{
}
public void Play()
{
System.out.println("Play Hockey:"+getName());
}
}
class Football_Player extends Player
{
Football_Player( String var)
{
}
public void Play()
{
System.out.println("Play Football:"+getName());
}
}
public class PolyDemo
{
public static void main(String[] args)
{
Player ref; // set up var for an Playerl
Cricket_Player aCplayer = new Cricket_Player("Sachin"); // makes specific objects
Hockey_Player aHplayer = new Hockey_Player("Dhanaraj"); Football_Player aFplayer = new Football_Player("Bhutia");
// now reference each as an Animal ref = aCplayer;
ref.Play();
ref = aHplayer;
ref.Play();
ref = aFplayer;
ref.Play();
}
}
Output:
Play Cricket:Sachin
Play Hockey:Dhanaraj
Play Football:Bhutia
Here is the complete solution for question 4 of MCS 024 assignment.