   [java] overriding functions?
  Hey, I would want to override a function that is private. I would want to do it like this:    class TestClass {public TestClass() {}private void howToDoSomeStuff() {//This is a description of an algorithm or somethingSystem.out.println("This is the standard howToDoSomeStuff()");}public final void doSomeStuff() {//This is the function that the user will call//Whenever he wants to do the stuff in howToDoSomeStuff()//First do some general stuff, and then call the//howToDoSomeStuff()howToDoSomeStuff();//If any overridden howToDoSomeStuff() exists, I want to call it...}}//This is how I want to create the objects//I understand that I'm making an annonymous class here//But still, the new howToDoSomeStuff() should be called//when I call it with test.doSomeStuff();TestClass test = new TestClass() {private void howToDoSomeStuff() {System.out.println("This is the overridden howToDoSomeStuff()");}}But the problem is that the overridden function is not called! The old one is called instead Is there any way of specifying that to always call the newest function?I could Imagine that it has something to do with the "private" stuff. Since I'm actually creating a new class there... So that the new function is not visible. And I know that I could make the function public and it would work great, but then there would be no point in making this - Since the idea is to not let the user directly call howToDoSomeStuff(), for security reasons...Thank you![edited by - bilsa on February 16, 2004 6:17:29 PM][edited by - bilsa on February 16, 2004 6:18:15 PM]
quote:Original post by bilsa  I could Imagine that it has something to do with the "private" stuff. Since I''m actually creating a new class there... So that the new function is not visible. And I know that I could make the function public and it would work great, but then there would be no point in making this - Since the idea is to not let the user directly call howToDoSomeStuff(), for security reasons...First off, forget security reasons.  If you think you can stop someone from calling a method just by making it private, you are mistaken.  Badly.  Real security is all about who has access to what in ALL ways.  It''s trivial to hack a class file to change a method''s visibility, so if they have access to the class files, you can forget about enforcing security through access modifiers.The reason access modifiers exist is for design.  By setting a method''s visibility, you indicate to users of the class when they should or shouldn''t be using that method.  But it''s just a recommendation.  Never think of it as more than that.And now to answer the rest of the question: There are more access levels than public and private.  One in particular exists for *precisely* this situation...  It''s called protected.  The basic idea of protected visibility is that it is visible to subclasses, but not to external classes.Java makes a bit of a mess of this concept though, by having protected methods visible to all classes in the same package, regardless of any subclass/superclass relationship.  I personally think that was a mistake on Sun''s part when they designed the language, and a lot of people agree.Java also has a fourth visibility level, which is called package or default visibility.  It''s the visibility level of methods (and fields, as all of this applies to fields as well) you get when you don''t specify a different visibility level.  Methods with package visibility can be seen and used only by classes in the same package.  A subclass that''s not in the same package doesn''t have access to package-visible methods, even though it would have access to protected methods. ;  Yes, I did try the protected variant since I also thought tha protected meant that it is visible to all subclasses but not external classes...But as you said I shortly noticed that I could freely use it wherever I wanted in the package...Well, I might have expressed myself wrong. I didn''t mean that kind of security I mean''t more like somewhat telling the user of my class to call the doSomeStuff() function instead of calling the core function directly... Maybe more a design appeal than security. I mean, to me it doesn''t matter that much if I make it public or whatever, since I know how it''s supposed to be used. But someone else using my class could be tempted to use tha core function directly which is unwanted...Thank you anyway 