aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes some questions on Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "some questions on Interfaces" Watch "some questions on Interfaces" New topic
Author

some questions on Interfaces

Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
1)
public interface Aquestion
{
void someMethod();
}

the correct answer is
the class which implemnts Aquestion should have someMethod which must necessarily public
my question is why can't it be default?
2)in what cicumstances can an interface be private or protected?
I appreciate any help with example
3)Abstract classes can have constructors why can't interfaces have constructors,when both canot be instantiated.
-thanks in advance
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tony kunds:
my question is why can't it be default?

From the JLS, §9.1.4 Access to Interface Member Names:

All interface members are implicitly public.

Therefore, it would be illegal to implement a method required by an interface as default because that would make the method "less visible" than the original method, which is not legal in Java.
2)in what cicumstances can an interface be private or protected?

Again, from the JLS, §9.1.1 Interface Modifiers:

The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration (�8.5) and are discussed in �8.5.1.

So protected or private interfaces are only involved when an interface is nested within another class. Check out those other sections of the JLS if you need more details about this.
3)Abstract classes can have constructors why can't interfaces have constructors,when both canot be instantiated.

Don't forget that an abstract class can have member variables of its own that are then inherited by any subclasses. Because of this, initialization of those members may be required, which requires a constructor.
An interface, on the other hand, has only static members, which requires no constructor.
I hope that helps answer most of your questions,
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

the class which implemnts Aquestion should have someMethod which must necessarily public
my question is why can't it be default?

It is not possible to narrow the access of a method by overriding it. Suppose a code is written using references of the base class. But, at runtime, these references may hold instances of the derived class, whose method could be called from fewer places than those from which the methods of the base type. This would be the case if overriding a method with a narrowing access would be allowed. What's the problem then ? the code was written with a certain level of access to the methods that invokes; however the methods that are really executed may not be called given that they have fewer access.

2)in what cicumstances can an interface be private or protected?
I appreciate any help with example

Just place an interface within a class.

3)Abstract classes can have constructors why can't interfaces have constructors,when both canot be instantiated.

The base abstract class constructor is helpful because it's executed whenever a derived class is istantiated. Thus providing common initilization.
[ May 06, 2002: Message edited by: Jose Botella ]
[ May 06, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some questions on Interfaces