Thursday, March 12, 2009

What is meant by upcasting (CoreJava)

Question :What is meant by upcasting? (CoreJava)
Answer :Upcasting is where a derived object reference is cast to one of its base
objects reference.
class Base
{
public void show()
{
System.out.println("In Base class");
}
}
class Derived extends Base
{
public void show()
{
System.out.println("In Derived class");
}
public static void main(String args[])
{
//upcasting
Base base = new Derived();
base.show();
}
}
When an object is upcast it becomes a base object for the purpose of the
cast, therefore any new fields and methods
declared in the derived class are not accessible.

No comments: