• 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
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Whats the result of the following code?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.. in Client.java


Choose the one correct answer.

1. Compilation Error at 1
2. Compilation Error at 2
3. Compilation Error at 3
4. Compilation Error at 4
5. The code compiles just fine and when executed, will result in "Default Gateway" as the output.

again this question for people who are going to take the exam. the gurus can always comment on responses. please give a chance for us taking the exam to answer first. thanks once again. Remember in the exam you are not allowed to use a compiler to check the validity of the code.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good question of the type found on the SCJP.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question, I got it wrong before I put it into eclipse. I won't put the answer down yet though, after a bit it'd be interesting to discuss.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am quite confused with this,
i guess the option should be b), because u r trying to set the accessibility of the method printName() to default.
Any say on it?
Am i right?
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right. But again this question was intended for people taking the exam. But i'd love to hear comments on this question from the gurus.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I'll be honest I didn't get it right either.

Moreover, I don't think that the K&B book covers the syntax for creating an inner class defined within an interface. Unless I just forgot about it...which is great news...especially with 18 days to go until the exam.

Cheers,

Si.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also guess it at 2) because i've a question here that whether can we give implementation of a method in an interface inside the interface itself(let it be inside the nested-static class)....
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading the book Programmer's guide for Java Certification by Mr. Khalid Mughal, when i was practising some code and i bumped into this error. To be honest even i was puzzled which lead me to think though it really close and it did make sense (had to ).

My experience so far:
I initially planned only 2 weeks for preparing for the certification. Step 1 i thought i could complete Mr. Khalid's book in 1 week and then in 2nd week i thought i could complete the K&B book. But its already 2 weeks, and i'm still half way through with Mr. Khalid's book. My experience with this book has been fabulous. But most of the time i'm writing code and trying different ways by tweaking the code here and there to break it and understand. I don't know if thats a good strategy as i get digressed so many times. But i'm hopeful to be done with this book by next week and then spring on to K&B. I'm hoping to take the test in the middle of April. Thanks for reading my post.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact that you've defined the method in the inner-class of the interface is fine. If you change the method to public all is good.

I'm a little confused on the semantics of this though. Everything there is of default access, I assume it has something to do with the fact that the method is defined inside the inner class. Anybody with some clarification?
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well very tempting so now it works

 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah even i too agree, K & B has not explained inner classes well, its given quite well in Khalid Mughal. But what i also think is that if K &B has not given it so descriptive may be that in exam not so tough questions appear. Thats what i think.
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


assume it has something to do with the fact that the method is defined inside the inner class


Even if it were defined in a class outside the package of the interface, the result is the same. I think it is do with fulfiling the contract that the interface publishes. I was under the impression that meeting the interface contract should be enough to implement an interface. But looks like under any circumstances the visibility of implemented methods should always be public. The same is true even if the class were abstract. Is that correct?
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i meant the implemented methods in an abstract class. i stand corrected.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By Steven,


Everything there is of default access, I assume it has something to do with the fact that the method is defined inside the inner class. Anybody with some clarification?


Everything defined over there is not of default access. The methods defined inside the interface is implicitly public and abstract while fields are public static final.
So in ur method u r trying to make it less readable. Thats why u r getting the error.
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh,
thanks for your explanation.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey rahul and others,

I am appearing for SCJP 1.4 after 4 days . I thought that the code will work fine. It just missed my mind that the method is getting restrictive from public to default.. i mean i know this concept etc but just it didnt click me
rahul if u have such gr8 questions pls do post



luv
aruna
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rahul thanx for the post..
I didnt really kno that we cud haf a class within an interface that implements the very same interface..But i suppose if a nested class cud extend the enclosing the class why shudnt this also work with an interface
This looked more like the error was of class implementing the interface rather than interface methods are by default public .Didnt even see it comming..
Thanx dude
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys,

just modifying the code little:



i've a entered a line of code "class a extends GatewayImpl {}" ........according to what i thought that since fields inside an interface are public static final by default so if we are defining a class inside an interface it should also be static(and definately it is) but i was not sure of whether the class will be treated as a field(ofcourse not a method) and hence it will be final also so that it's non-extendable.....but to my surprise it can be extendeds....so i was wrong in thinking this....

can anyone tell me that what is a class, deifined inside an interface or may be a class itself, is treated like......a class is actually a user defined datatype so when we are defining a class inside a class or an interface what are doing?? how can we relate the concept of a class being usr-defined data type inside a class or an inerface.

plz clarify........
thanx
amit
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,


according to what i thought that since fields inside an interface are public static final by default so if we are defining a class inside an interface it should also be static(and definately it is) but i was not sure of whether the class will be treated as a field(ofcourse not a method) and hence it will be final also so that it's non-extendable.....but to my surprise it can be extendeds....so i was wrong in thinking this....


yes u r wrong, class inside an interface is considered only to be a static class and not a static final.


can anyone tell me that what is a class, deifined inside an interface or may be a class itself, is treated like......a class is actually a user defined datatype so when we are defining a class inside a class or an interface what are doing?? how can we relate the concept of a class being usr-defined data type inside a class or an inerface.


U know that when u define a class inside a top-level class, i mean inner class u can relate that class only with the instance of the top-level class, right?
Now suppose u define a class inside an interface, how would u relate the class to the interface?
Using an instance of the interface, NEVER EVER(u cant do that),
then how will u do it.
So that has to be a static class inside an interface.
Now u got it.
Remember in Java, its not necessary to remember all the things and infact u cant do that, but u can derive ur explanation from whatever u have learnt. So remember less and derive most.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic