Exercise – 8 (Runtime Polymorphism)
a). Write a JAVA program that implements Runtime polymorphism
class One
{
int a,b;
void sum()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter a and b values:");
a=s.nextInt();
b=s.nextInt();
int sum=a+b;
System.out.println("Sum of two numbers in the class 'One' is:"+sum);
}
}
class Two extends One
{
int a=90,b=67;
int add;
void sum()
{
add=a+b;
System.out.println("Sum of the numbers in the class 'Two' is:"+add);
}
}
class Poly
{
public static void main(String args[])
{
One obj=new One();
obj.sum();
One t=new Two();
t.sum();
}
}
b). Write a Case study on run time polymorphism, inheritance that implements in above problem
Polymorphism:-
Polymorphism is the process of performing the single task
in different ways. Polymorphism is the Greek word . In which ‘poly‘ means
many and ‘morphs
’ means forms. It means many forms .
Polymorphism
is classified into two types they are-
1. Static
or Compile time Polymorphism
2. Dynamic
or Run time Polymorphism
Runtime
Polymorphism:-
Runtime Polymorphism is also called as Dynamic
polymorphism. It is the process of calling the Overriding methods in runtime
rather than In compile time.
In this process the overriding methods are called with reference
of super class.
Explanation:-
In the
above program we use single inheritance two achieve the runtime polymorphism in
our example.
We use two classes here. We call the base class with the
reference of the super class.
We create different objects
to the two classes and call the both base class as well as super class with reference of
super class only.
So, Runtime polymorphism is achieved here.
It is important to note that relying solely on Java assignment help may not be enough for beginners to become proficient in the language. People must try themselves. Dissertation Proposal Writing Services
ReplyDelete