Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it is necessary to mark the method as public , when implementing the mathod of interface ?




 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise the abilities of interfaces would be severely reduced. E.g. if we would make it private we can only use it in the same class, if we would make it default only in the same package, if we would make it protected it wouldn't work at all since a class cannot extend an interface. So that leaves public.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Otherwise the abilities of interfaces would be severely reduced. E.g. if we would make it private we can only use it in the same class, if we would make it default only in the same package, if we would make it protected it wouldn't work at all since a class cannot extend an interface. So that leaves public.


interface i1
{
void method();
}

class A implements i1
{

public void method() // you have to mark this method as public , other modifier won't work with it . Try it.
{}

public static void main(String args[])
{
A a=new A();
}
}
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface methods are implicitly public. Interface variables are implicitly public static final.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface methods and variables are public by default. When we implement the method of an Interface in the child classes It should be public. that means As per one of the rules of Interfaces, the access specifier in the implemented classes should not be weaker than the previous one. for example, we can change the access like private to any other modifier. the order is
private-->protected-->default-->public
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private-->protected-->default-->public

I know this .
but the the punching question is that
WHY THE JAVA CREATER NEED TO CREATE THIS TYPE OF LIMITATION (when inplement the interface the method must be public)?
had you ever think about it ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHY THE JAVA CREATER NEED TO CREATE THIS TYPE OF LIMITATION

Hi Munjal, I dont see it as a limitation. Since you make it mandatory as public then other classes extending the class need not implement the interface once more. or in other words, it can use the methods implemented by the parent class for sure knowing it would be only public.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think protected access is more restrictive than default/package-private? It is the other way round.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
munjal upadhyay wrote:WHY THE JAVA CREATER NEED TO CREATE THIS TYPE OF LIMITATION

KeepItDown please.
 
munjal upadhyay
Ranch Hand
Posts: 69
Java Mac OS X Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
WHY THE JAVA CREATER NEED TO CREATE THIS TYPE OF LIMITATION

Hi Munjal, I dont see it as a limitation. Since you make it mandatory as public then other classes extending the class need not implement the interface once more. or in other words, it can use the methods implemented by the parent class for sure knowing it would be only public.


ok
I don't mean that to restrintion of the data (like private or public etc..) . but the restrction that , without public you can't define the method(in the class while implementig the interface).
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the restrction that , without public you can't define the method(in the class while implementig the interface)


These are the rules:
- when implementing an abstract method, you cannot reduce its visibility.
- when overriding a method, you cannot reduce its visibility.

The reason for these rules (or restrictions as you call them) is that the subtype must be an equal or even better substitute for the supertype. For example anything a List can do, an ArrayList can do equally or even better.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ogeh Ikem wrote:. . . the subtype must be an equal or even better substitute for the supertype. . . .
That sound just the same as what I said a few minutes ago here.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Campbell Ritchie. The same logic applies when dealing with exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic