Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

force a class to use a method

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is there a way to force a class to use a method it has inherit from the parent class?. I mean, without having to implement it.

Regards
Jorge
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make it final?

M
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the parent class has an abstract method then the child class has to implement it in order to compile.

If the parent class has a method that it's not abstract nor final or private, then the child class inherits it, but it's not forced to use it as it happends when inheriting from abstract classes or implementing interfaces.

My question is that if it's possible to make sure that all the child classes use the method they inherits. I've never seen it. If not, is there a way to ensure it with other techniques?

thanks in advance
Jorge
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand.

If the method is final, it IS inherited.
The subclass may not overwrite it.

If the method is private, it is inherited too, of course.
But to avoid overwriting, you use final.

Of course you don't force the class to USE the method this way.

But you can call the method in every constructor.
So the subclasses are forced to use it.

Why do you need it?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stefan Wagner:
If the method is private, it is inherited too, of course.



I thought making a method private prevents inheritance? I thought only public and protected methods could be inherited.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[SW]: I still don't understand.

If the method is final, it IS inherited.


Unless it's private.

The subclass may not overwrite it.

Correct. Where "overwrite" is interpreted as "overload or hide".

If the method is private, it is inherited too, of course.

Not according to the JLS. I suppose it all depends on how you define inheritance I suppose, and in past discussion various people have suggested alternate definitions which would allow a different answer. But my feeling is, unless you specify a specific definition of the word, the default assumption should be to use the definition given by the JLS. Otherwise there's really no definite answer, and further discussion is useless.

But to avoid overwriting, you use final.

Yup.

I'm not really sure what Jorge actually want here. It's trivial to prevent other classes from providing alternate definitions of a method - just use final, as Max said. But if the goal is to actually force classes to use you method (as opposed to mrerely preventing them from using alternate definitions of your method) - well - that's pretty tough. Perhaps you could make some sort of Ant task which searches a class for references to a given method, and breaks the build if none are found. Or fail if a JUnit test does not successfully call the given method at some time during its execution.

To be honest, this whole idea sounds pretty silly to me, which probably means I haven't understood what Jorge really wants. (Unless it was the question Max answered long ago.)
[ July 21, 2004: Message edited by: Jim Yingst ]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
The subclass may not overwrite it.

Correct. Where "overwrite" is interpreted as "overload or hide".

Did you mean to say "override or hide"? Because final methods can be overloaded (i.e. you can create another method of the same name, as long as it has a different number or type of arguments).

- Peter
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:


I'm not really sure what Jorge actually want here. It's trivial to prevent other classes from providing alternate definitions of a method - just use final, as Max said. But if the goal is to actually force classes to use you method (as opposed to mrerely preventing them from using alternate definitions of your method) - well - that's pretty tough. Perhaps you could make some sort of Ant task which searches a class for references to a given method, and breaks the build if none are found. Or fail if a JUnit test does not successfully call the given method at some time during its execution.


[ July 21, 2004: Message edited by: Jim Yingst ]



That was exactly what I meant. And that's what I had thought, that it was not easy to do. But I like your ideas of using Ant or Junit.
Thanks for the info :-)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to take a look at the Template Method pattern.
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain how the Template Method patters works?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[PdH]: Did you mean to say "override or hide"?

Yes, thanks for the catch.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jorge Blaz:
Can you please explain how the Template Method patters works?



http://home.earthlink.net/~huston2/dp/templateMethod.html

On a second thought, it doesn't sound exactly what you are after. On the other, I don't quite understand *why* you would want to force a class to call a certain method. This sounds kinda like a strange requirement to me. Perhaps you could elaborate on that? Thanks!
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example (a really simple one) of what I mean:







Classes ChildOne and ChilTwo implements the method doOk and doError, each one with it's own implementation.
But they inherit the method validateIfStringIsUppercase is already defined in Parent class, and they use it.
What i would like is to force that class ChildOne and ChildTwo has two use the method validateIfStringIsUppercase to make the decision of calling doOk or doError.

Thanks
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let the constructor call the test.
Call the mandatory-methods in the childs directly.

The constructor will force the subclasses to call 'super' explicitly, and give an argument.

The question is, whether it is possible, to know the parameter allways, when the constructor is called.

Jim: I wasn't aware of the JLS-usage of the expression 'to inherit a method'. I don't think it clears much. I may inherit money and do with it what I want - and I inherit physical attributes, which I cannot change; this would make sense to me.
But of course in a community, we cannot use our own definitions, just because we think they make more sense. So I will start using it the way sun does.
Perhaps I find time to look, what Stroustrup tells for c++...
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could design your code as follows:





 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mani's solution is the Template Method pattern. The superclass defines a policy by implementing a method which defers specifics of the implementation by calling abstract methods which are defined in subclasses.
 
Corey Hollaway
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the protected do? I've tried using protected with public methods, and they all inherit the same. And they both seem to have the same protection as far as other classes using protected methods VS using public methods. Thank you!
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what protected does:



How's that?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the parent class should call validateIfStringIsUppercase and call the subclass for the inputs to the same.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I realized that Mani already suggested that.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is you haven't stated all the constraints on what you're trying to do (maybe you're not clear on them yourself). You obviously don't want to force a subclass to use a method willy-nilly, whenever. You want that method to be invoked at the proper moment.

Well, what moment? It's not enough to say I want the subclass to have to call this method, you have to say when and in what situations you want the method called. This is sounding increasingly to me like an event-based approach might address your problem.

Here's what you do: the parent class' constructor registers an event listener with some outside object that contains the knowledge of when to kick off that method call. The parent also declares a method (could be abstract, if you want the subclass to implement it), which that event listener calls when the time is nigh.

Subclass extends the parent, is forced to implement any abstract methods (if any), and when the event fires the event listener registration is already taken care of and out of the hands of the subclass. The method gets invoked.

Did I understand the problem you're trying to solve?

sev
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!