It's not a secret anymore!
The moose likes Beginning Java and the fly likes Prevent a method from being inherited- from the SUB class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prevent a method from being inherited- from the SUB class" Watch "Prevent a method from being inherited- from the SUB class" New topic

Prevent a method from being inherited- from the SUB class

Chad Schultz

Joined: Mar 25, 2007
Posts: 23
I know this is a silly question, but I'm wondering. I'm overriding the FilterWriter class in order to create a FixedWidthWriter class that will accept objects and convert them to flat-file format. Every new write() method accepts the object and the field width; the methods truncate or fill with spaces as necessary.

However, I realized that if a programmer (yes, I accidentally did this) calls the write(int) method instead of write(int,int), the program will cheerfully write the integer as a character, instead of converting the integer to its string value and correcting it to the proper field width.

I could, of course, override the write(int) method and throw a custom exception, but that seems a silly way to do it. Is there an easy way to override the method by making it invalid, so that the compiler will point out any call to write(int) as having the incorrect arguments?

Bupjae Lee
Ranch Hand

Joined: May 14, 2007
Posts: 107
If you declared a method with final modifier, any subclass can't override that method.
Chad Schultz

Joined: Mar 25, 2007
Posts: 23
Thanks, but I don't have access to the superclass. I only have access to the class that is doing the inheriting. Is there a way from the inheriting class, the subclass, to prevent it from inheriting one of its superclass' methods?
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
You don't have to throw a custom exception. There's already an UnsupportedOperationException you can use. See the Javadoc for Iterator.remove() for an example of its use.

Alternatively, you override the single parameter method and call the two parameter method with a default value for the second paarmeter. As long as you document that this is what will happen and what the default value is, there should be no problem.

Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
There is no such way, and the premise is bad.

Liskov Substitution Principle suggests that a subtype should be able to be substituted for a supertype without clients noticing. That's clearly not what you want. Your class is a subclass because it's easy to reuse functionality from another class if you write a subclass of it.

However, ease and simplicity are not the same thing.

A simpler way would be to make a class whose instances hold an instance of what used to be the superclass, and then you can control what methods you expose. The fact that you'll be writing some methods that just delegate through to the wrapped object is an annoying side-effect, but not one that shows a problem with the mechanism; more with the language. This is called composition. Joshua Bloch says to favour composition over inheritance. I'd say to favour anything over implementation inheritance.

With programmatic access to methods such as that offered by AOP, or more flexible languages than Java, such as Lisp, Smalltalk, Ruby, Python, but not C++, you could implement all the delegate methods automatically.

In C++, you can have private inheritance, in which case you are only inheriting for code reuse, and the inheritance doesn't leak out of the class (no other code can tell you're inheriting). I just checked the details of it - look at what 'using' does in this:
I agree. Here's the link:
subject: Prevent a method from being inherited- from the SUB class
It's not a secret anymore!