aspose file tools*
The moose likes Beginning Java and the fly likes can we have private methods in an abstract class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we have private methods in an abstract class?" Watch "can we have private methods in an abstract class?" New topic
Author

can we have private methods in an abstract class?

vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
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

Joined: Nov 16, 2005
Posts: 502

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.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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

Joined: Jun 16, 2006
Posts: 387


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


R.N
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What would you do with a private method in an interface?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
wouldn't you like to know...


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we have private methods in an abstract class?