wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes implementing interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "implementing interfaces" Watch "implementing interfaces" New topic
Author

implementing interfaces

Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
Why is the method which implements an interface method declared public?
Why cannot we use the default access specifier?

my code is----

interface addition
{
void add(int i,int j);
}


class summation implements addition
{
public void add(int i,int j)//this line will give an error if no public access specifier is used
{
int a=i+j;
System.out.println("The Sum is "+a);
}
public static void main(String args[])
{
summation s=new summation();
s.add(6,7);
}
}


Jitendra
SCJP1.5
SCWCD1.5
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, just because that's the rule. Java has only the C++ notion of "public inheritance", and not "private inheritance" -- if a class extends another class or implements an interface, then those details can not be hidden from any clients.


[Jess in Action][AskingGoodQuestions]
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
If i am not mistaken,there is a reason to everything!
i am at a loss to know why cannot protected or default access specifier be used in above case
[ February 08, 2007: Message edited by: Jitendra Jha ]
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
All the methods of an interface are considered public (and also abstract), even when that is not explicitly stated.

Your interface:


means exactly the same as


This means indeed that all implementing classes also must have those methods public.
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
Sorry for being so stupid!!
i think that a method in interface can only be public automatically if the interface is also public,else it will have a default access specifier!!
correct me if i am wrong!If i am right,them the problem still stands.

also the last reply raised another question,
While overriding methods,is it mandatory to use the same access specifiers?I guess only return type,method_name and parameters need to be same?

please help
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Originally posted by Jitendra Jha:
Sorry for being so stupid!!
i think that a method in interface can only be public automatically if the interface is also public,else it will have a default access specifier!!


No, all the methods in an interface are always public. Whether or not the interface is public is not relevant to this.

Originally posted by Jitendra Jha:

also the last reply raised another question,
While overriding methods,is it mandatory to use the same access specifiers?I guess only return type,method_name and parameters need to be same?


Well, two things.
Here we are talking about implementing a method of an interface, not overriding one.
When overriding you do not have to use the same access specifier, just not a more strict one. (You can override a protected method in a subclass with a public one, just not with a private one)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In fact the rules for implementing an inherited abstract method and overriding one are exactly the same - you cannot reduce visibility of the method.

The reason is Liskov's Substitution Principle - the new type needs to be usable everywhere the subtype is usable. And for that, all operations that clients can access from the supertype also need to be accessable in the subtype.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi,
one more point ,
while overriding a method, we can't narrow down its accesibility
i.e. public to protected or private or default can't be done.
but the other way is allowed, i.e. you can make protected to public.

this applies to general overriding .

a good one, but nor quite clear about this rule.

hope anyone help???


thanks,<br />gaurav abbi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: implementing interfaces
 
Similar Threads
Method Overloading Examples
doubt in interface
Why primitive arguement's value won't be changed even it's changed in method
Public methods
Use Of Interface