• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Practices (Static Methods with OOP)  RSS feed

 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going through Head First Java and am at ch 10, Numbers and Statics.
I found myself asking a question that wasn't addressed in the book.
If I create a static method in a superclass can I access it via the subclass name?


So I played around a bit and created a superclass "Animal" with a static int called "count" which is incremented in the Animal constructor and a static method called "getCount()" that returns the static variable "count."

I then created a subclass called "Dog" and created a few Dog objects.

I was able to access "count" in Animal using either "Animal.getCount()" or "Dog.getCount()."

I am assuming that it would be a best pracitce to only reference the static method via the superclass "Animal." However, is there a way to prevent someone accessing that method via a subclass?

By that I mean if someone extends Animal into a Cat class and then uses "Cat.getCount()" could I prevent that from the Animal class?

Is this making sense or am I just rambling?

Thanks!

-Mike
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access modifiers are the key

"default"
private
public
protected

Take a look at the "default" access modifier - smarter people than me have called this the package access modifier.

base/Animal.java

base/UseAnimal.java

sub/Cat


So another class in the same package as Animal can call getCount(). But no class in any package (even base) can call getCount() on a Cat reference. But a class in the same package as Animal (base) can cast Cat to an Animal and call it.

Have fun! This is the tip of the ice burg.

[ September 20, 2006: Message edited by: Tim LeMaster ]
[ September 20, 2006: Message edited by: Tim LeMaster ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim LeMaster:
Take a look at the "default" access modifier - smarter people than me have called this the package access modifier.

Quite often that is because there is no such thing as the "default". Package access is a type of access - the default access is sometimes package, but not always.
There are four types of access scope in order of least restrictive to most; public, protected, package, private. 'package' access cannot be explicitly specified. The default access for most - but not all - contexts is package. Where it is not, package access is not permitted.
 
Michael Farinha
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replys. I think I understand the concept... I should place my classes in a package and give them "default" access and then make sure to override the "getCount()" method in each subclass that I create.

I suppose a follow up question would be, is this something to really worry about while coding?

Thanks again!

-Mike
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite often that is because there is no such thing as the "default".

Care to give an example? The Java tutorial has this to say...

If a class has no modifier (the default, also known as package-private)
...
At the member level, you can also use the public modifier or no modifier (package-private)


And the K&B SCJP book seems to jive with this.

I should place my classes in a package and give them "default" access and then make sure to override the "getCount()" method in each subclass that I create.

Well that depends on the behavior you want. Something called getCount() I would normally expect to be "public" so the users of your class can call it. Honestly I don't find myself using no modifier very often - my properties tend to be private - with public mutators (get/set), sometimes protected for things I don't want others calling but want people to be able to override in a subclass, and private if I really want everyone to leave it alone.

suppose a follow up question would be, is this something to really worry about while coding?

Absolutely, your access modifiers determine who can do what to your classes.
All things being equal I usually have private properties and public methods, unless I have a good reason to change it.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Tony was getting at is there isn't just one "default" visibility. What the "default" is depends on context. For an interface method it's public, for a class it's package-private.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim LeMaster:
Quite often that is because there is no such thing as the "default".

Care to give an example? The Java tutorial has this to say...

If a class has no modifier (the default, also known as package-private)
...
At the member level, you can also use the public modifier or no modifier (package-private)

Hi Tim,
Perhaps I should qualify that I do not accept the Sun tutorial or any books (Head First or last or whatever) as any kind of authority. In fact, I might consider arguing that there is more fallacy than otherwise in these kinds of texts if I were so inclined. I also think that a critical analysis that exists independently reveals something more. Simply, I can find as many fallacious remarks in those texts that you mention as truthful ones, which I believe undermines the refutation of "but the text says this". It is unfortunate that this is the case and I don't wish it were just so that I can point my finger at it with snide remarks, but I must accept it as it is.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't plainly except information in books either - I tend to write alot of test code. However you don't offer any evidence for your case, so I also don't accept your statements at face value.

Forunately someone else gave a great example of such a case (Interfaces in which the only legal access modifer is public - so this is the default).
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link below hopefully should clarify some of your doubts-

http://blogs.sun.com/prats/entry/java_oops_session_pesit

Please feel free to pen down your Feedbacks regarding presentation in comment section.
[ September 22, 2006: Message edited by: Pratibha Malhotra ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pratibha Malhotra:
Link below hopefully should clarify some of your doubts-

http://blogs.sun.com/prats/entry/java_oops_session_pesit

Please feel free to pen down your Feedbacks regarding presentation in comment section.

[ September 22, 2006: Message edited by: Pratibha Malhotra ]



Dear Pratibha,

Check your PM please...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim LeMaster:
I don't plainly except information in books either - I tend to write alot of test code. However you don't offer any evidence for your case, so I also don't accept your statements at face value.

Forunately someone else gave a great example of such a case (Interfaces in which the only legal access modifer is public - so this is the default).


Need I? You did it yourself?

Should I continue? I challenge the reader to do so.
I don't feel at all compelled to qualify my statements simply because some book purports misinformation - inadvertantly or not - also without basis.
The sky is red.
 
Bert Bates
author
Sheriff
Posts: 8945
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

With all due respect, why don't you go write yourself up a little Java book? We've all read a lot of your ideas and maybe we'd benefit from your insights more if you took the time to get your thoughts organized and in book form. Additionally, you could be a beacon for all the technical writers in the world who struggle so mightily with errata and less than perfect explanations.

Seriously Tony, give it a shot!
[ September 25, 2006: Message edited by: Bert Bates ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!