Total members 11894 |It is currently Thu Nov 21, 2024 11:06 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.???




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

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.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

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



Topic Tags

Java OOP
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com