some one give d clear explanation for polymorphism

Answer Posted / anjani kumar jha

Java supports two types of polymorphism
1)compile time
2)Run time

1)Compile:---overloading is the best example of compile-
time polymorphism.
Look at this example:----

class NewClass {

public void aSum()
{
System.out.println("hi.......");
}
// NewClass nc1=new InheritClass();
// NewClass nc=new NewClass();
}
class InheritClass extends NewClass
{
@Override
public void aSum()
{
System.out.println("over-riden......");
}
public static void main(String as[])
{
NewClass nc=new NewClass();
NewClass nc1=new InheritClass();

nc.aSum();.......compile time polymorphism
reference type determine which method to be invoke
//nc1.aSum();
}




2)Run time:Over-ridding is the example of run time
polymorphism........

look at example----
class NewClass {

public void aSum()
{
System.out.println("hi.......");
}
// NewClass nc1=new InheritClass();
// NewClass nc=new NewClass();
}
class InheritClass extends NewClass
{
@Override
public void aSum()
{
System.out.println("over-riden......");
}
public static void main(String as[])
{
NewClass nc=new NewClass();
NewClass nc1=new InheritClass();

// nc.aSum();
nc1.aSum(); //object type determine which method
shuold call
//run the example u will get idea about that
}
}
}

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can you inherit a private class?

629


What are objects in oop?

601


What are the 4 pillars of oop?

660


if i have same function with same number of argument but defined in different files. Now i am adding these two files in a third file and calling this function . which will get called and wht decide the precedence?

2746


Plese get me a perfect C++ program for railway/airway reservation with all details.

3419






hi all..i want to know oops concepts clearly can any1 explain??

1675


What is byval and byref? What are differences between them?

1680


What is a class oop?

585


Can destructor be overloaded?

591


Why can't we have instance(stack) of a class as a member of the same class like eg.Class A{A obj;} as we can have self refential pointer

1609


just right the logic of it 1--> If few people are electing then every time ur candidate should win 2--> arrange books in box, if box carry weight == books weight then take another box..... find the no of box required.

6479


What is an advantage of polymorphism?

585


What is use of overloading?

601


What is pointer in oop?

528


program for insertion ,deletion,sorting in double link list

2274