• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritence Problem..  RSS feed

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference variable can be assigned a reference to any subclass derived from superclass but the reverse in not true . Why ??

Class first
{.....}
Class second extends first
{...........}

Class mainone
{
public static void main (string[] args)
{
second sec = new second();
first fir = new first();

sec = fir //Error
fir=sec ; // Works
}
}

Why ??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a "second" object is a "first" object as well, but a "first" instance is not an instance of "second".

Consider what would happen if you could do this, and then called one of the methods that "second" declares which "first" doesn't have.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks Sir, thanks a lot....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!