• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Interface ...need help

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1.An Interface can never be private or protected.
True
False

The answer is given : False.But I think the ans. must be true.An Interface must be public.
-----------
2.Interface methods can be declared with the following modifiers
public
none (i.e., no access modifier).
private.
static
native
synchronized.
The answer is given public amd none(default). BUT I think it should be only public.
-------------
In Addition:
Can some1 explain me the exact rule(including exceptions) of multiple immplementation of Interface for a class !! pls....its may be easy for u but pls. explain it for me.

Thanks in advance.
Ratul Banerjee

SRC. : http://www.angelfire.com/or/abhilash/quiz/quiz6.html
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ratul,
try to put your interface inside the toplevel nested class(static) and give it any modifir it work's fine.

2)if u declare a method inside an interface it's upto u whether u put a public modifier or not infront of the method declaration.
It get's a default access if u don't put any modifier infront of the method declaration .Remeber varaible declaration inside an interface is implicitly public static final.Now, have a look at code below.
interface i
{
void lap();
}
public class fit extends i
{
public void lap()
{
}
}
now see when u provide an implementation to the method declared in an interface u have to strictly provide a public keyword before the method in the subclass otherwise u will get a compile time errod method not declared public inside the suclass(fit in our case).
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as far as your third question is concerned, u can implement as many interface to any class.This is what i have understood from your question,please be clear about your question's and put more matter regarding that.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ratul,
This is regarding ur 2nd q:
I tried following code:
public interface AccessMod {
private void run();
}
and got compilation error:
Interface methods can't be native, static, synchronized, final, private or protected.
This means you can have either public or none modifier for methods.
-Seema
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nitin and seema.

[This message has been edited by ratul banji (edited April 22, 2001).]
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI nitin,
For the 1st one--->
As u told :
try to put your interface inside the toplevel nested class(static) and give it any modifir it work's fine.

I have done it both in jdk1.2.1 and jdk 3 . But ,dear it is not working.I think I am mis-understading something.If u spend some more thime and show me a good example--that will be very kind of u.
As fer as 2nd q. ---> sorry for that I didn't mean 2 post this q.
Thanks in Advance.

Ratul Banerjee.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer Q#1 :
You can create nested interface just like you create nested class (private & protected are allowed).
Answer Q#2 :
This is what Bruce Eckel (TIJ) said :
"...You can choose to explicitly declare the method declarations in an interface as public. But they are public even if you don�t say it..."
Exact Rules Multiple Inheritance :
You place all the interface names after the implements keyword and separate them with commas. You can have as many interfaces as you want�each one becomes an independent type that you can UPCAST to.
Composition = Has a
Inheritance(extends) = is a
Interface(implements) = is-like-a
hope that helps
stevie
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stevie for ur answers.
For the 1st one:
I have tried but unable 2 do it.pls..give me an example so I can understand it properly.
thanks.
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ratul..., here is the code :
class A {
private interface X {
void f();
}
public class B implements X {
public void f() {
System.out.println("implements Private Interface");
}
}
public X getX() { return new B(); }
private X xRef;
public void receiveX(X x) {
xRef = x;
xRef.f();
}
}
public class NestingInterface {
public static void main(String[] args) {
A a = new A();
a.receiveX(a.getX());
}
}
Note :
thing's to remember, creating Private interface is allowed ONLY inside class, you can not creating private interface inside another interface, because everything inside interface are implicitly public.
method : public & abstract
field : public, static, & final
hope that helps
stevie
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the below given matter will clarifies u'r confusions regarding interfaces
when u r writing an interface remember three things
1. all methods in an interface are public and abstract
2. all fields in an interface are public, static and final
the above two points are implicit things.
3. none of the methods in an interface can be private, protected, static, synchronized, and native. coming to the fields part of an interface, more precisely a native and synchronized modifiers cant be applicable to fields. only a field can be public , static and final which is an implicit representation. since a field is a final, u must initialize the variable, no default initialization is done.
for further clarification send me your queries.
thank you.

------------------
 
ratul banji
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks stevie .
I am surprised ...ur code is working fine.Thank u very much.
Accually, I didn't know this feature of interface.
thanks.
srinivasrao ..thanks 2 u also.
 
reply
    Bookmark Topic Watch Topic
  • New Topic