Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some questions on Interfaces

 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic