Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

derived class reference cannot point to an object of base class

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gopi Chella
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ".
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My fault , Ritchie!

I mean if we cast a subclass to superclass.
it gives runtime error.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah Ritchie,

there will be no compiler error.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic