Hello: I am very new to the world of JAVA, or the world of programming for that matter. So, please forgive me if this question sounds elementary. My understanding is that the Object Class is the ultimate superclass, in that it is inherited by all other classes. In looking closely at the Object Class, I see there are a number of public instance methods, and 2 protected instance methods. I do not understand the logic of differentiating between public and protected in this particular class. Since the Object Class is inherited by all other classes, I would have thought that the public methods and protected methods would have exactly the same visability. Can anyone explain to me why certain methods of the Object Class have been designated as protected?
Hi, Keith Why the Sun people did "protect" those two methods is unclear. But, anyway, all subclasses have access to and can override the "protected" members of their parent class. Have a look at the Specifications for Object at: http://java.sun.com/docs/books/jls/html/javalang.doc1.html#46442 I think that the clone() and finalize() methods are aimed to be overriden by their subclasses.
Great question. Basically the functionality between public and protected modifiers in the Object class is the same as far as visibility, you can access both directly. So your question as to why some are protected and some public is answered by the definition of the modifiers themselves. Public methods should be used to define the public interface (API) for the given class. Protected methods should be used for methods that aren't necessary for the use of the class, but may be of interest to any inherited classes. So it is a matter of style and not pure functionality. Regardless of how the Object class is implemented, it was written using the modifiers properly. This is good advice to programmers. Regardless of the scope or implementation, objects should always be coded properly. Then when scope and implementation DO CHANGE, there is much less work involved. Thank you and a squishee a day makes your tongue red.
Joined: Sep 05, 2000
Thank you for your reply, Apu: The definition of the modifiers that you provided is quite helpful, and expands my understanding of their use a little. But I am still confused about how to put into practise the theory of the various modifier definitions. I wondered if you could expand a little on how one determines when a method should be part of the public interface to the class (API), versus when the method may be of interest to inheriting classes, but is not necessary for the use of the class? I am guessing this is a decision made by personal taste or gut feel. But I wondered if there was a more scientific way to make such a determination. Continuing with the Object class as an example, what specifically makes the clone() and finalize() methods unnecessary for the use of the class, while all the other methods are considered necessary elements of the API? When doing my own programming, should it be obvious to others why I chose a specific modifier? Or is that of little consequence in actual programming practise? Again, I realize these sorts of questions may be something that is only picked up through experience. But any advice that can be offered on the best use of the various modifiers would be greatly appreciated. For example, what types of things should one be asking themself when deciding upon a specific modifier for a method or variable? Take care, Keith
Originally posted by Apu Nahasapeemapetilon: Great question. Basically the functionality between public and protected modifiers in the Object class is the same as far as visibility, you can access both directly. So your question as to why some are protected and some public is answered by the definition of the modifiers themselves. Public methods should be used to define the public interface (API) for the given class. Protected methods should be used for methods that aren't necessary for the use of the class, but may be of interest to any inherited classes. So it is a matter of style and not pure functionality. Regardless of how the Object class is implemented, it was written using the modifiers properly. This is good advice to programmers. Regardless of the scope or implementation, objects should always be coded properly. Then when scope and implementation DO CHANGE, there is much less work involved. Thank you and a squishee a day makes your tongue red.
I am very new to Java ( I've been reading for about 2 weeks only) so correct me if Im wrong. Cloning of objects is potentially dangerous and is not to be used wantonly. Therefore the Clone() method is made protected. Say we have two classes - Class A and Class B. If we wanted to clone an object of Class A, we would, in all probability, want to do it from within an object Class B. Now, even though both Class A and Class B are extended from the superclass Object, one is not extended from the other, and therefore a protected method in Class A would not be available in Class B UNLESS IT WAS OVER RIDDEN explicitly. This affords some kind of protection against cloning, unless the designer of the Class explicitly privides for it. Please let me know if Iam right (or where the flaw lies) Thanks and Good Luck Ambi my Email is email@example.com
Joined: Sep 06, 2000
Keith, The use of modifiers is determined by the modifier and its scope and potential. How to apply them are basically up to the style of the programmer. Strong understanding of OO priciples will point one in the correct direction. Experience is invaluable in designing class models. I was fortunate that I learned OO from one of the best. One thing she taught me was common sense and simple logic will easily show one the way. This is all very abstract and really isn't offering anything to really hold onto. First let me address some of your questions and then I'll try a simple example. Basically the public interface is how one communicates with the world (the world being other objects). This interface can be very much like people. Some are very quiet and don't talk much (small interface), others are very chatty (big interface). Mostly this should be determined by the functionality and requirements of the object. The scientific way is that it makes sense (remember the experience). Ambi, basically answers your question about the clone() and finalize() method. They are not necessary as many never use these methods. They also can be overridden in descendants as needed. So, Ambi you are correct. When you do your own programming, ask yourself one question, can others look at this code and make sense of this. The beauty of good programmers is not the complexity of the code, but the simplicity. Far better to have 20 lines of easily understandable code versus 5 lines of "what the f**k?". This applies not only to modifiers, but ALL code.
It's getting late so let's move on to a small example. The example is the game Minesweeper. We have a couple objects, the window (with menu, scoring and the playing field) and then the square object (can be clicked, can hold bomb or count of bombs around me). Wait a minute, I should have you do this. This can be done differently, but... Now we know the window needs to know about the squares, but do the squares need to know about the window? This is an issue of scope and can handle many ways. For example there has to be an init or start method to start the game. If the window handles this (most likely, as this is where the menu exists) then it needs to tell the squares to reset, thus we knoww the squares need the reset method to be public. But what scope is the window's start method? Public?, Private?, Protected? It could be any of them, but why wouldn't we choose the most appropriate. Public is out because the squares don't need to call start (if the menu was its own object, then this could be argued, but I already defined as property of window object), and no other objects exist in this little world. Protected, why? No inheritance exists. Private, most likely. Why? because we have good reasons why it is not the others. Private actually is appropriate because tthe window can do all of this itself, especially since calls to the squares public reset method. If you wanna take a stab at some other functionality, try some with modifiers and I'll tellyou what I think. BTW, the event listeners on the squares for mouse clicks is public. End droning diatribe.
Joined: Sep 05, 2000
I want to thank all of those who responded to my initial question. I think the details in each of your responses will be quite helpful for me as I progress in reading and writing JAVA code. Take care, Keith