File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract class and interfaces. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract class and interfaces." Watch "Abstract class and interfaces." New topic
Author

Abstract class and interfaces.

Sanjeev Ba
Ranch Hand

Joined: Dec 31, 2006
Posts: 40

I have the following questions of Interfaces and Abstract classes.

1. Why is a protected interface not allowed?

2. Interface methods are implicitly public and abstract, can I ?

3. Why can't I specify "default" access specifier for an interface (it is only implicit when I do not say "public", but I cannot say "default interface test{}"

4. What happens when I do not provide the "public" keyword for an interface and it does not belong to any package? Who can access it?

5. I have a group of Interfaces and Abstract class (.java) files in a folder. I do javac *.java - how does the compiler resolve the order before compiling?

Regards
Sanjeev
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Sanjeev Bhima Rao wrote:1. Why is a protected interface not allowed?

You mean declaring the interface protected or the methods inside the interface as protected? No top level class/interface/enum can be declared as protected.
2. Interface methods are implicitly public and abstract, can I ?

Can you what?
3. Why can't I specify "default" access specifier for an interface (it is only implicit when I do not say "public", but I cannot say "default interface test{}"

default is not a valid access specifier. default keyword is used in switch-case statement not to specify scope of an object (applies to classes as well).
4. What happens when I do not provide the "public" keyword for an interface and it does not belong to any package? Who can access it?

Only other classes which are not in any package (applies to classes as well)
5. I have a group of Interfaces and Abstract class (.java) files in a folder. I do javac *.java - how does the compiler resolve the order before compiling?

The compiler will compile the classes/interfaces in alphabetical order and if it comes to a class/interface which uses another class/interface, that one is compiled first...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sanjeev Ba
Ranch Hand

Joined: Dec 31, 2006
Posts: 40

1. I meant the interface (not the methods). I need to know why.

2. Sorry for missing the question. Can I have protected interface methods? Why?

Thanks for the answers to the rest of the questions.

Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Ankit Garg wrote:
Sanjeev Bhima Rao wrote:4. What happens when I do not provide the "public" keyword for an interface and it does not belong to any package? Who can access it?

Only other classes which are not in any package (applies to classes as well)



In practice though, aren't the classes which aren't explicitly in a package put in a "default" package? I seem to recall reading this somewhere...

// Andreas
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Andreas Svenkson wrote:In practice though, aren't the classes which aren't explicitly in a package put in a "default" package? I seem to recall reading this somewhere...

Yes, that's right. But you can't import the default package, so only other classes that are in the default package can use them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class and interfaces.