Message Board


Message Board > Java > methods default public?

November 28, 2006, 17:15
DTM
Earthling!
823 posts

Do java methods default to public? They seem to, but maybe they default to protected and I don't know the difference. :what:
But protected would make them only usable to sub classes. :S

Also is a class public by default?

[Edited on November 28, 2006 by DTM]
____________
:o
#
November 28, 2006, 19:33
Frimkron
Frustrated Megalomaniac
703 posts

If you dont specify public, private or protected they actually use a different, 4th type which I can't remember the name of.

But this type doesn't allow access from other classes unless they're in the same package, if I remember correctly.

[Edited on November 28, 2006 by Frimkron]
____________
#
November 28, 2006, 19:50
Eckolin
Quite Whiskered
388 posts

A friendly mode?
____________
Maker of Games...
Wisdom is supreme; therefore get wisdom.
Need help with coding? I probably wrote something similar.
#
November 28, 2006, 21:03
PEader
お前はもう死んでいる
1486 posts

Quoting Frimkron:
If you dont specify public, private or protected they actually use a different, 4th type which I can't remember the name of.

But this type doesn't allow access from other classes unless they're in the same package, if I remember correctly.

It's just called the default signified by the lack of public, private and protected modifiers, and the access is only by package as you say.

Here is a tip: protected in java means that other classes of the same type can access the method or member. In C++ protected means only the class and it's descendents can use the method or variable.

So to answer the original question everything defaults to default. By not declaring a modifier the method can only be accessed from within the class and from within the package the class is in.

Also a class is the same. When you declare a class with no modifier it is only accessible from within the package or class. I don't think you can declare classes private unless they are inner classes. So classes can only have no modifier or public (unless they are inner).

Reference: Controlling Access to Members of a Class

[Edited on November 28, 2006 by PEader]
____________
I see 57,005 people.
#
November 29, 2006, 03:00
Sandman
F3n!x0r
1194 posts

Quoting Frimkron:
If you dont specify public, private or protected they actually use a different, 4th type which I can't remember the name of.

Wasn't this restricted?
____________
BennuWiki
Yes, my avatar has grey borders in IE (so get a decent browser)
ROOFLEZ ROOFLEZ
#
November 29, 2006, 14:43
DTM
Earthling!
823 posts

Thanks!
____________
:o
#
December 11, 2006, 14:45
Dennis
どこかにいる
2097 posts

I advice you to use these terms always, even when it is not required for the sake of your classes'/use cases' visibility and responsibilities. You should encapsulate your data as much as possible. it also depends on the method. An abstract method defaults to public, in case you are using polymorphism.
____________
Kwakkel
#
December 11, 2006, 20:57
PEader
お前はもう死んでいる
1486 posts

Quoting Dennis:
I advice you to use these terms always, even when it is not required for the sake of your classes'/use cases' visibility and responsibilities. You should encapsulate your data as much as possible. it also depends on the method.
Is this meant to be advice? You need to clarify your point Dennis. You might as well tell people to code good or not to make typos.

Quote:
An abstract method defaults to public, in case you are using polymorphism.

Why would an abstract method default to public when nothing else defaults to public? Do you have any references where this is stated? Like I said the default scope is class and/or package if none of public, private or protected is used.
____________
I see 57,005 people.
#
December 12, 2006, 12:30
Dennis
どこかにいる
2097 posts

Typos get noticed by a compiler, optional keywords don't. optional keywords with a strong meaning are dangerous. Like in Fenix I type "int" before all my variables even when it is int by default, just because it prevents making errors.

Quoting PEader:
Why would an abstract method default to public when nothing else defaults to public? Do you have any references where this is stated? Like I said the default scope is class and/or package if none of public, private or protected is used.

because people like you complain about it?

Why is observer an abstract class and observable an interface? while all other keywords ending -able are abstract classes and all ending with -er are interfaces?

Why are bananas crooked?
____________
Kwakkel
#
December 12, 2006, 14:15
PEader
お前はもう死んでいる
1486 posts

Quoting Dennis:
Typos get noticed by a compiler, optional keywords don't. optional keywords with a strong meaning are dangerous. Like in Fenix I type "int" before all my variables even when it is int by default, just because it prevents making errors.
That is what I meant, you need to explain yourself rather then just throwing things out there. It would be even better if you gave an example of the danger rather then expecting everyone to take it at face value.

Quote:
Quoting PEader:
Why would an abstract method default to public when nothing else defaults to public? Do you have any references where this is stated? Like I said the default scope is class and/or package if none of public, private or protected is used.
because people like you complain about it?

Why is observer an abstract class and observable an interface? while all other keywords ending -able are abstract classes and all ending with -er are interfaces?

Why are bananas crooked?


I don't know what you mean by "people like me" but I don't want to see someone giving other people bad advice in order to make themselves look good. So instead of being smart about it show me something where Sun Microsystems say abstract methods default to public instead of trying to imply some sort of shit about who I am.


The Sun documentation states that default access is not the same as public. This has already been covered in this thread. Now you say it doesn't do this on abstract methods and your proof is to mention a naming convention. So I am in a quandry here as to who to believe on this. Should I trust Sun Microsystems (who control the language) or Dennis (who doesn't know his arse from his elbows). Who do you think I'm going with when it comes to the Java language? Or is this all a big understanding with you misrepresenting the word default and thus confusing me?

[Edited on December 12, 2006 by PEader]
____________
I see 57,005 people.
#

Message Board > Java > methods default public?

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!