File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we have private methods in an abstract class?

 
vijay kumarg
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it is possible to declare private methods in an interface?
I studied as , by default all the methods in an interface ar public abstract.
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. you cannot.

Interface is specifies contract which class should adhere when it implements it. so, private method in interface does not make any sense.
 
Svend Rost
Ranch Hand
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

welcome to the ranch.


I think it would have been faster to write two test programs yourself
compared to posting this question on the Ranch.

Can I have a private method in an abstract class?


Can I have a private method in an interface?



/Svend Rost

p.s. you remember correctly: Interfaces only allow public and abstract
 
Rohit Nath
Ranch Hand
Posts: 387
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


NewInterface.java:16: modifier private not allowed here

One advice please try to run these code yourself first and see compilation errors yourself it is of no use if someone tells you directly. It can be fun to see compiler errors yourself at times.

Thanks!
Rohit Nath
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your subject line asks if you can have private methods in an abstract class -- the answer to that is "yes". As other folks have noted, you can't in an interface.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that methods declared in an interface are automatically public, even when you don't specify an access specifier:

Note that in a class where you implement this interface, you must make the method public, otherwise you get an error:
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you do with a private method in an interface?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wouldn't you like to know...
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic