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 main method override?

826


i am getting an of the type can not convert int to int *. to overcome this problem what we should do?

2052


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<

1629


Which language is pure oop?

744


What is a class in oop?

768


How can you overcome the diamond problem in inheritance?

955


What is Difference Between Inheritance and creating object and getting data? means Class A extends B{ B.getMethod();} (OR) Class A{ b obj=new B(); obj.getMethod(); }

2198


What is difference between pop and oop?

799


Why is oop useful?

808


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

1882


Why do we use class in oops?

740


Please send ford technologies placement paper 2 my mail id

1839


Why do we use class?

811


Which method cannot be overridden?

757


How many human genes are polymorphic?

779