Basic concept: Polymorphism is a mechanism which allows to have many forms of the method having the same name.
Polymorphism
Basic concept: Polymorphism is a mechanism which allows to have many
forms of the method having the same name. That means, a method A() may take an
instance of an object of one class and may execute its method or the same
method A() may take another instance of an object and may execute the method
belonging to another class. In the following Java program we have taken the
superclass A and build a multilevel inheritance.
Using
object of corresponding class corresponding method is invoked by the same
function fun.
The
implementation is given by following Java program -
Java
Program[Polymorphism Demo.java]
class A
extends Object
{
public
String toString()
{
return
"A";
}
}
class B
extends A
{
public
String toString()
{
return
"B";
}
}
class C
extends B
{
public
String toString()
{
{
return
"C";
}
}
public
class PolymorphismDemo
{
public
static void main(String[] args) {
fun(new
C());//invokes the method toString() of class C
fun(new
B());//invokes the method toString() of class B
fun(new
A());//invokes the method toString() of class A
}
public
static void fun(Object x) {
System.out.println(x.toString());
}
}
Output
F:\test>javac
PolymorphismDemo.java
F:\test>java
PolymorphismDemo
C
B
A
Program
Explanation
The same
function fun is used to execute the methods of various class. The selection of
the method of the class (i.e. toString method) depends upon the instance of the
object which is passed to the function fun. Which implementation is to be used
is determined dynamically by the Java Virtual Machine. This mechanism is called
dynamic binding.
The
dynamic method dispatch is also called as runtime polymorphism. During the run
time polymorphism, a call to overridden method is resolved at run time. The
overridden method is called using the reference variable of a super class(or
base class). The determination of which method to invoke is done using the
object being referred by the reference variable.
Following
is a simple Java program that illustrates the Run time polymorphism. class Base
{
void
display()
{
System.out.println("\n
Base Method Called");
}
}
class
Derived extends Base
{
void
display() //overridden method
{
System.out.println("\n
Derived Method Called");
}
}
public
class RunPolyDemo
{
public
static void main(String args[])
{
Base
obj=new Derived();//obj is reference to base class // which is referred by the
derived class
obj.display();
//method invocation determined at run time
}
}
Output
D:\test>javac
RunPolyDemo.java
D:\test>java
RunPolyDemo
Derived
Method Called
Program
Explanation:
In above
program, the display method is an overridden method because with the same
signature we are modifying it in its derived class. This method is invoked in
the main(). function using the reference obj. This reference variable is of
superclass type. It is assigned with the reference of Derived class. At run
time it is decided that the display method of derived class should be invoked.
Hence it is known as run time polymorphism.
Ex. 2.11.1: Declare a class
called book, having author_name as private data member. Extend book class to
have two sub classes called book_publication and paper_publication. Each of
these classes have private member called title. Write a complete program to
show usage of dynamic method dispatch (dynamic polymorphism) to display book or
paper publications of given author. Use command line arguments for inputting
data.
Sol. :
import java.util.Scanner;
class
book
{
private
String[] author_name = {"Puntambekar","Godse"}; Meesa &
tan
void
display()
{
System.out.println("Author
names: ");
for(int
i=0;i<author_name.length;i++)
{
System.out.println("\t"+author_name[i]);
}
}
}
class
book_publication extends book
{
private
String[][] title = {{"Java Programming","Compiler Design", "DAA"},{"Digital
Electronics","Computer Graphics", "Microprocessor"}};
void
display(int j)
{
System.out.println("Books
published by Given Author are...");
for(int
i=0;i<3;i++)
{
System.out.println(title[j][i]);
}
}
}
class
paper_publication extends book
{
private
String[][] title = {{"AAA","BBB"},{"XXX",
"YYY"}};
void
display(int j)
{
System.out.println("Papers
published by Given Author are...");
for(int
i=0;i<2;i++)
{
System.out.println(title
[j][i]);
}
}
}
class
MainClass
{
public
static void main(String[] args)
{
Scanner
s = new Scanner(System.in);
book obj
=new book();
book_publication
bookpub=new book_publication();
paper_publication
paperpub=new paper_publication();
int
author_name=0,choice;
if(args[0].equals("Puntambekar"))
author_name=0;
else
author
name = 1;
System.out.println("\n
1.Books Published\n 2.Paper Published");
System.out.println("\n Enter Your Choice:
");
choice=s.nextInt();
switch(choice)
{
case 1:
bookpub.display(author_name);
break;
case 2:
paperpub.display(author_name);
break;
default:
nevi vd
bedatidug adfood")mb
System.out.println("wrong
choice....");
System.exit(0);
}
}
}
Output
Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : with Example Java Programs - Polymorphism
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation