wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can we have inner interfaces ...!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can we have inner interfaces ...!?" Watch "Can we have inner interfaces ...!?" New topic
Author

Can we have inner interfaces ...!?

Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
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


Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42
Look at the section
Nested Interfaces

cheers.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Look at the code:




Regards,
cmbhatt


cmbhatt
madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
For question 5, answers are 1 & 2.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Aug 13, 2006
Posts: 178
Yes, you're correct. I mistook it for interface constants. Thanks
Steven Young
Ranch Hand

Joined: Apr 11, 2007
Posts: 36
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

Joined: Aug 13, 2006
Posts: 178
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

Joined: Feb 28, 2007
Posts: 1707
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.
Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51

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.


SCJP 1.4 - SCJP 1.6 - SCWCD in progress
madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we have inner interfaces ...!?
 
Similar Threads
Nested Interface
anonymous inner classes
MULTIPLE INHERITANCE IN JAVA
Interfaces with same method
regarding interface