• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam LAb Question on Arrays

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




objects that can be put to A can only be of type C

why ??
Capture2.PNG
[Thumbnail for Capture2.PNG]
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no thats not true
objects that can be put to A can only be of type C


you can put objects of type A ,B or C

B extends A
C extend B
means C extends A
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I ask, can you refer a Super class object by a sub class reference?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumit kothalikar wrote:no thats not true
objects that can be put to A can only be of type C


you can put objects of type A ,B or C

B extends A
C extend B
means C extends A

You are wrong! Check the below code.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please see the explanation

i tried to put new A() into a[0]
compilation fails
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:please see the explanation

i tried to put new A() into a[0]
compilation fails

Compilation fail? No. Check it again please.!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@mohit..

I donot think your line no 6 will compile because a has already been declared in above line it will give compile time error...Please check it......
and one more thing..

You can only refer superclass to sublcass..

Subclass referece cannot be refer to a superclass Object........These are the restriction put in java..
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:please see the explanation

i tried to put new A() into a[0]
compilation fails


It will compile because a[0] is the reference type of class A........
share the error which you get

May be you have done the mistake in typing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic