• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem on Polymorphism

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic