Polymorphism Interview Questions and Answers
Polymorphism Interview Questions and Answers
The major differences between Polymorphism and Inheritance in Java are listed below:
2. One of the advantages of Inheritance is code reusability in child class. The child class
inherits behavior from the parent class whereas Polymorphism enables the child class to
redefine already defined behavior inside the parent class.
}
public class Test{
public static void main(String[] args){
PolymorphismDemoClass parent = new PolymorphismDemoClassChild();
parent.m1(new PolymorphismDemoClass());
}
}
b. Runtime error
c. Prints One
c. prints int
d. prints Integer
a. Integer
b. Integer...
b. prints Long
c. Runtime Exception
d. None
b. long
c. int
d. Runtime Exception
b. Long
c. Integer
d. Runtime Exception
b. prints Long
c. prints Double
d. Runtime Exception
b. Long
c. Double
d. Number
b. prints Long
c. prints Double
d. prints Integer
b. long
c. byte
d. Integer
b. int
c. Runtime error
b. long
c. long Integer
d. Integer
c. byte: 5
d. Runtime error
// Driver code
public static void main(String [] args)
{
Test10 obj = new Test10();
obj.overloadedMethod(null);
}
}
b. prints Double
c. prints Integer
d. prints String
b. Double
c. Integer
d. String
1) Related to person
2) Related to product
Run-Time Polymorphism
class Addition
System.out.println(a+b);
System.out.println(a+b+c);
a.add(10,20);
a.add(20,5,6);
Output: 30
31
(7) What is method overriding?
In java, whenever we have same name method in parent
and child class with the same number of arguments and
same data types is known as method overriding in java.
Method overriding is the example of dynamic
polymorphism.
For example:
class Parent
{
void show()
System.out.println("Parent");
void show()
{
System.out.println("Child");
}
public static void main(String args[])
{
Parent p =new Child();
p.show();
}
Output: Child
For example:
Parent p = new Child();//upcasting declaration