• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

implementing interfaces

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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,
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???
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic