• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Inheritence Problem..  RSS feed

 
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 ??
 
Rancher
Posts: 42975
76
  • 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!