Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class and interfaces.

 
Sanjeev Ba
Ranch Hand
Posts: 40
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sanjeev Ba
Ranch Hand
Posts: 40
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic