Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Polymorhism and UpCasting

Fri Oct 24, 2008 1:00 pm

Is There any Difference between UpCasting and Polymorphism.. i m
confused.. please let me understand.. i searched Google, Books..

in Polymorphism..
basereference = new Derived(); // UPCast automatically by Java..

Derived derived = new Derived(); // This is UpCasting..
((Base)derived).method();

What the Difference Between them.. Both do the same thing.. then whats
the difference.???



Re: Polymorhism and UpCasting

Fri Oct 24, 2008 1:05 pm

There are two types of polymorphisms in Java. Compile time and Runtime
polymorphism.
so whatever the code snipets u have given are examples of runtime
polymorpism or u can say Upcasting.
Because methods(variables) from which of the classes (base or derived) will
be called is decided at the runtime only.

Upcasting takes advantage of polymorphism. An example of polymorphism:

Code:
class A {
}

class B extends A {
}

B can be referred to as an object of type A or B.

Upcasting works like this:
B aBobj = new B ();
A anAobj = (A)aBobj;
Because aBobj can be referred to by the class names B and A, this code
will never fail. You might say that upcasting and type extension is the
implementation of polymorphism in Java.

Post a reply
  Related Posts  to : Polymorhism and UpCasting
 difference between upcasting and downcasting     -  

Topic Tags

Java OOP