• 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

Can we have inner interfaces ...!?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends
i came across inner classes then i got a doupt that can i have inner interfaces. then i did the following example. It is working fine....surprise!!?

Code:
package innerClasses;

public class Foo{
public static void main(String[] args){
new FooBarImpl().method1();
}
public interface FooBar{
public void method1();
}

}

class FooBarImpl implements Foo.FooBar{
public void method1(){
System.out.println("this is from FooBarImpl");
}
}

so, i need some clarity that "Inner class" means an object in another, it may be class or interface. Am i right?

regards
Mallik
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the section
Nested Interfaces

cheers.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the code:




Regards,
cmbhatt
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
below questions are from examulator mock exam site and some from other sites which I didnot remember
6 questions on topic Interfaces
my answers were followed by 6th question.
I am having doubts specified along with answers.
you can compare with your answers and give reason why my answer is wrong?(if it is wrong)
and please clarify my doubts.

Q1.Which of the following is correct? Select the two correct answers.
A.The native keyword indicates that the method is implemented in another language like C/C++.
B.The only statements that can appear before an import statement in a Java file are comments.
C.The method definitions inside interfaces are public and abstract. They cannot be private or protected.
D.A class constructor may have public or protected keyword before them, nothing else.

2.Which of the following are true about interfaces. Select the two correct answers.
A.Methods declared in interfaces are implicitly private.
B.Variables declared in interfaces are implicitly public, static, and final.
C.An interface can extend any number of interfaces.
D.The keyword implements indicate that an interface inherits from another.




3) Given the following code, which of the options if inserted on the line after the comment //here will allow the code to compile without error?



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


4)1my doubt is with option 2- class Moodle is not using implements Remote, can't we create an object of Remote and just leave without implementing test?



The interface cant be instantiated. Only classes can be instantiated. So you can't use new keyword ever with interface, except for creating a anonymous class.


6) 4 I knew the Garbage collection is there in SCJP1.5 but, the questions will be such deeper? we should know the System class. I haven't seen in any books I read.



The question is not outside the book knowledge. The last second page of the garbage collection topic in K&B book mentions System.gc() is the method used to request garbage collection. Hence 4. is the correct choice.

If you tell me what doubts you have about the other answers , I can try to help with them.

Thanks,
Megha
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For question 5, answers are 1 & 2.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Krishnan:
For question 5, answers are 1 & 2.



Krishnan, Did you read the question carefully?


5) Which of the following can legally be inserted into an interface?

1)
public static String getPostCode();
2)
public abstract String getName();
3)
private String getAverageHousePrice();
4)
public void setPostCode(String s) { System.out.print(s); }



Option "2" is the only legal method signature that can be inserted into an
interface.

1) interface method can't be static
3) interface method can't be private, it is public by default,
4) interface methods don't have body




Thanks,
cmbhatt
[ April 28, 2007: Message edited by: Chandra Bhatt ]
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're correct. I mistook it for interface constants. Thanks
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1 asks for 2 correct answers.

I make it A and C.

Can't be B because package statement can appear before an import.
Can't be D because you can have a private constructor to prevent other classes from instantiating an object in your class (e.g Factory design pattern).
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For question 1, answer A is only partly correct. In my understanding the languages don't have to be C or C++ and the first part of the answer is correct.
Any thoughts?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Krishnan:
For question 1, answer A is only partly correct. In my understanding the languages don't have to be C or C++ and the first part of the answer is correct.
Any thoughts?



native methods means these method have been defined in some other languages as C or C++, which may be plate-form dependent. You can find so many methods in the Thread class, System class and so on, for example where methods are native; It is well to keep them on C or C++ like language for performance issue or due to any kind of dependency on the operating system.
 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by M Krishnan:
For question 1, answer A is only partly correct. In my understanding the languages don't have to be C or C++ and the first part of the answer is correct.



I think answer A in question 1 didn't say that the languages have to be C/C++, only like C/C++.

Regards,
Marcos.
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by megha joshi:
Hi,



The question is not outside the book knowledge. The last second page of the garbage collection topic in K&B book mentions System.gc() is the method used to request garbage collection. Hence 4. is the correct choice.

If you tell me what doubts you have about the other answers , I can try to help with them.

Thanks,
Megha



Thanks Megha,
I was having doubts with 4 and 6 only.
Please can you help me in telling, the comment 'we can instantiate interface while creating anonymous classes only', is it available in K&B?
Please let me know the page number.

Thanks All,
so can I consider my remaining all answers are right except question 1.
where
ans for Q1 is A & C.
[ April 30, 2007: Message edited by: madhu v pe ]
reply
    Bookmark Topic Watch Topic
  • New Topic