• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubts in access modifiers  RSS feed

 
surya goutham datta rejeti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.Why members of an interface are always public? If an interface is declared to be package-private(default) What is the use of having its members public.
2.Why finalize method is protected? Anyway it is going to be inherited by all classes in all packages.
 
Malcolm Storey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Am unsure what you mean by
an interface is declared to be package-private.
An interface is at the same level as a class and visible to all. Its methods are public cos an interface is used by an object to present a specific face to the world. It is a way for other objects to access this object, so would be of little use if private or protected.

2. finalize is protected to stop it being called from anywhere but a subclass. Basically finalize is "rubbish" anyway!! It can't be relied on to be called so forget it - if you need to do something on disposal, write a method for it [eg dispose()] and call it yourself.
[ November 17, 2008: Message edited by: Malcolm Storey ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A package-private interface can be implemented by a public class in the same package, and that class can be extended by other classes in other packages.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it can't be cast to the interface - since outside the package, that interface does not exist.

Compare it with StringBuilder. It extends package-default AbstractStringBuilder, but is that information available from code? Not quite.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To restate the same information that's already been presented:

1. Why are members of an interface always public?


It should be obvious why they are never private. Declaring a private interface is a meaningless statement. If you are "inside" a class, you can see what is private, and don't need any interface telling you what to do.

Since protected is only available to subclasses, defining a protected method in a class effectively imposes the interface that subclasses need to implement (if you define protected abstract methods in an abstract class).

The only reason for an interface is to define a desired public interface to multiple unrelated classes. (there are actually other reasons - but their not applicable to your question)

2. Why is the finalize method protected?

All classes will have a finalize method, but they cannot invoke the method on other classes. Indeed, they shouldn't invoke it on themselves either, but that can't be restricted by the interface. It will be invoked externally, and it can be overridden - in which case it should invoke super's implementation of it to correctly implement it.

(As noted above, if you are aware that an object is ready to be disposed of, it's best to clean up its resource use immediately rather than wait for any garbage collection cycle.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!