There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
...why does the following code throws ClassCastException
Exception in thread "main" java.lang.ClassCastException: Beta cannot be cast to Delta
class Wind implements Instrument {}
class Brass extends Wind {}
class Trumpet extends Brass {}
class Horn extends Brass {}
interface Animal {}
class Mammal implements Animal { }
class Bovine extends Mammal {}
class Cow extends Bovine {
Tarun Oohri wrote:...but I could not understand one thing ...
class A{}
class B extends A{}
class C extends B{
public static void main(String...s){
C c = (new C())(new B())new C();
}
}
Here we are first casting c into b...which is OK.. Now at this stage ..the object is of b...ie.
C c = (new C())b; where b refers to B object.
Now how can we cast B into C ???
sai rama krishna wrote:
English is my second language. I wonder how Bovine is related to Cow. Are both same. Also how Trumpet is related to Brass( is it a metal? or instrument). Also how Horn(i only know one horn which is Animal has horns) is related to Instrument. Also Wind means Air right not any kind of instrument? Please advise
Henry Wong wrote: a Bovine IS-A Cow, but a Cow is not necessarily a Bovine.
Junilu Lacar wrote:
Henry Wong wrote: a Bovine IS-A Cow, but a Cow is not necessarily a Bovine.
Actually, a Cow IS a Bovine. Not all Bovines are Cows though. Some Bovines are Buffalo.
Henry Wong wrote:
Sorry. Read the attached code backwards... have a cow ...
Are you sure it is that way round? I thought it was the other way round.Henry Wong wrote: . . . a Bovine IS-A Cow, but a Cow is not necessarily a Bovine. . . .