• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface  RSS feed

 
amir Ghannad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
Can I get a help on this? I am not sure about my answer can some one explain this for me? how is this valid or invalid




which of the following is valid or invalid?
valid


invalid
valid

valid

valid
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi amir,

In class hierarchy, a reference of superclass can be used to refer to object of subclass. But vice-versa is not true.

Thus,is invalid. Rest of your answers are correct.

I hope this helps.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand why these are valid or not, you have to understand what inheritance means in Java.

Inheritance means specialization. A subtype (a type that extends another type) is a special kind of its supertype.

For example, a Clock is a special kind of Timekeeper, an AlarmClock is a special kind of Clock, and a StopWatch is a special kind of Clock.

With this knowledge it's easy to see what is and what is not valid.

AlarmClock clock = new Clock(); is not valid. An AlarmClock is a special kind of Clock, but ofcourse not every Clock is an AlarmClock. So you can't assign a general kind of Clock to the special kind AlarmClock.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!