aspose file tools*
The moose likes Java in General and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

why it is necessary to mark the method as public , when implementing the mathod of interface ?




Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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

Joined: Oct 25, 2008
Posts: 2700

Interface methods are implicitly public. Interface variables are implicitly public static final.
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

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


Regards,

RK
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 13, 2005
Posts: 38888
    
  23
Why do you think protected access is more restrictive than default/package-private? It is the other way round.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

munjal upadhyay wrote:WHY THE JAVA CREATER NEED TO CREATE THIS TYPE OF LIMITATION

KeepItDown please.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
munjal upadhyay
Ranch Hand

Joined: Sep 18, 2010
Posts: 69

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

Joined: May 13, 2002
Posts: 180
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

Joined: Oct 13, 2005
Posts: 38888
    
  23
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

Joined: May 13, 2002
Posts: 180
That's right Campbell Ritchie. The same logic applies when dealing with exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance