wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem on Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem on Polymorphism" Watch "Problem on Polymorphism" New topic
Author

Problem on Polymorphism

Bonny Sen
Greenhorn

Joined: May 09, 2006
Posts: 26
Consider the following piece of code:

class Base
{
public void show(){
System.out.println("BASE");
}
}

class Derived extends Base
{
public void show(){
System.out.println("DERIVED");
}
}

public class BaseDerived
{

static public void main(String[] args)
{
Base base = new Base();
Derived derive = new Derived();

base = derive; // 1
base.show(); // 2

derive = (Derived)base; // 3
derive.show(); // 4
}
}
Could anyone answer the followind questions:

Q.1.: If I delete line 1 and run the program the output is:
BASE
java.lang.ClassCastException at BaseDerived.main(BaseDerived.java:26)
Exception in thread "main"

Q.2: If I run the above code as such, with out deleting any lines the output
is:
DERIVED
DERIVED


With Love, Bonny
"Life is like an ice-cream, Enjoy it before it melts..."
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Subhabrata Sen:
Consider the following piece of code:

class Base
{
public void show(){
System.out.println("BASE");
}
}

class Derived extends Base
{
public void show(){
System.out.println("DERIVED");
}
}

public class BaseDerived
{

static public void main(String[] args)
{
Base base = new Base();
Derived derive = new Derived();

base = derive; // 1
base.show(); // 2

derive = (Derived)base; // 3
derive.show(); // 4
}
}
Could anyone answer the followind questions:

Q.1.: If I delete line 1 and run the program the output is:
BASE
java.lang.ClassCastException at BaseDerived.main(BaseDerived.java:26)
Exception in thread "main"

Q.2: If I run the above code as such, with out deleting any lines the output
is:
DERIVED
DERIVED


In the first line of the main method, base is a reference to a Base object.

In the second line of the main method, derive is a reference to a Derived object.

Since Derived is a subclass of Base, it is legitimate to have base become an alias of derive which is what happens in line 1.

Then you can call the show method on base and it will call the method which was overridden. The runtime type of the object is still Derived.

You can also then legitimately cast the type of the reference of base to a Derived reference because base is already a reference to a Derived object.

If you do not have line 1, you will get a compile-time error because you cannot cast base, which is a reference to the Base object, to be a reference to a Derived object.

A Base object is not a Derived object, however a Derived object is a Base object.
[ June 15, 2006: Message edited by: Keith Lynn ]
Leonardo Luiz
Ranch Hand

Joined: Mar 24, 2006
Posts: 51
Q.1.:

The first output line is �BASE� because base is a Base type reference variable referring to an instance of Base. So �System.out.println("BASE");� is executed.
The next line prints an exception occurred on line 3. At this point the reference variable base is referring to a base instance, so this cast is not valid. A super class instance is not the same of a sub class instance.

Q.2.:

The first line is �DERIVED� because base is referring to an instance of Derived. So line 2 makes a polymorphic call to show() and �System.out.println("DERIVED");� is executed. The second one is �DERIVED� because the same overrided version of show() is executed on line 4. Note that the cast on line 3 is valid because the instance referenced by base IS A instance of DERIVED.


Best Regards (sorry for my poor English),

Leonardo Luiz
 
Consider Paul's rocket mass heater.
 
subject: Problem on Polymorphism