Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Inheritance

 
Ranch Hand
Posts: 69
Mac OS X Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why it is necessary to mark the method as public , when implementing the mathod of interface ?




 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface methods are implicitly public. Interface variables are implicitly public static final.
 
Ranch Hand
Posts: 63
Spring Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74630
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think protected access is more restrictive than default/package-private? It is the other way round.
 
Sheriff
Posts: 22563
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74630
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right Campbell Ritchie. The same logic applies when dealing with exceptions.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic