aspose file tools*
The moose likes Beginning Java and the fly likes derived class reference cannot point to an object of base class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "derived class reference cannot point to an object of base class" Watch "derived class reference cannot point to an object of base class" New topic
Author

derived class reference cannot point to an object of base class

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
If there are two classes base and derived, then we cannot do like this:

derived d = (derived)new base();

derived d = new base();

We are getting a compile time error in both of above statements.

Can anybody tell me the reason for this?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30967
    
158

Raj,
What is the compiler error? Can you show the class definition showing derived extending base?

Also, are your classes really named "derived" and "base"? It is traditional to use uppercase for the first character?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gopi Chella
Ranch Hand

Joined: Apr 26, 2010
Posts: 53
Raj,

Like Jaenne suggested the class names should start with Upper case(First Character).

However, First statement will get compile but it will give "ClassCastException" in runtime and the second statement wont compile at all.

To understand this then read "Java OOPS concepts ".

SCJP 1.5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Raj Kumar Bindal wrote:Can anybody tell me the reason for this?

Subclassing indicates an "is a" relationship between the superclass and the subclass - but it only goes in one direction. Let's give the classes more concrete names instead of "derived" and "base" to make it more clear:

If you think about it, it's no suprise that you can't write:

Because not every animal is an elephant.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

We cannot cast base class to super class!
Gopi is right.
the IDE will suggest you to cast it into super.
but will give ClassCastException in runtime.


Good luck!!
A small leak can sink a Gigantic ship.>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Raza Mohd wrote:We cannot cast base class to super class!
Why are you all using non-Java nomenclature? The Java terms are "superclass" and "subclass". Which is supposed to be th bease class and which the superclass? I think that question is at best confusing, but one can always cast a subclass object to its superclass.

Why you should want to cast to a superclass, may be another interesting question.
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

My fault , Ritchie!

I mean if we cast a subclass to superclass.
it gives runtime error.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Correct, a ClassCastException is thrown. But it will most definitely compile, because of the following example:
If you would attempt that with a new Tiger() you would indeed get an exception, because no matter how hard you tell it, a tiger will never behave like an elephant.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

yeah Ritchie,

there will be no compiler error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Raza Mohd wrote:My fault , Ritchie!

I mean if we cast a subclass to superclass.
it gives runtime error.
do you mean the other way round? Casting to a subclass can give all sorts of probkems.The last line will not compile because you are trying to fit an Animal (could be Tiger) into an Elephant space.
 
Don't get me started about those stupid light bulbs.
 
subject: derived class reference cannot point to an object of base class