• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance query  RSS feed

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{

}

class B extends A{

}

class C extends B{

}

when i do
1) A a= new B(); it compile and run fine
but even when i do
2) A a =new C(); then still it compiles and run fine

for case 2) is there any relationship between A and C (via B,if there is) due to which it compiles?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for case 2) is there any relationship between A and C (via B,if there is) due to which it compiles?


C ISA A because C extends B extends A
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Mammal {
}
class Dog extends Mammal {
}
class Chihuahua extends Dog{
}

same hierarchy. Chihuahuas ARE dogs, and therefor ARE mammals. it is legal to say

Mammal mamalRef = new Chihuahua();

since a Chihuahua MUST be a mammal.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If A<-B & B<-C then A<-C
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!