• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

package

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was solving a mock exam from niko's blog where i came accross this question:


These classes are defined in separate files. Will this code compile successfully? (1 correct answer)

package dir1;
public class Parent {
public java.util.Set<String> set;
}

package dir2;
public class Child extends dir1.Parent {
void test() {
set.add("Hello");
}
}

1. Yes.
2. No.



assuming dir2 is not a subpackage of dir1(because if it would be a subpackage,it would have been package dir1.dir2),the answer to this question is "no" right?? but they have marked the answer as yes-ie the code will compile...now how is that possible??
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you say so...? Have you tried the same?
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:Why you say so...? Have you tried the same?


say what?? why do i say that the answer is no?..iv already written that..because if it would be a subpackage,it would have been package dir1.dir2
or else the child should import the parent package otherwise the compilation would fail.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sumedha rao wrote:...or else the child should import the parent package otherwise the compilation would fail.



Do you see any difference here? You import things to avoid writing the fully qualified names in the code (and some other advantages too). This would give no compiler error if the class path is set to the Parent when the Child is compiled since the fully qualified name is used for the Parent class. Best thing is to try it yourself...
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sumedha, please do not post directly to Mock Exam Errata forum without discussing it on the SCJP forum at the first place.
I'm moving this back to the SCJP forum.
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic