• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract function, which does not specify the type of its argument  RSS feed

 
Jakob Kaiser
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I am wondering if the following is possible in Java:
I would like to write an abstract class which forces every concrete subclass to implent a function myfunction() that takes one argument, but the abstract class should not specify the type of the argument. So it should say something like: "If you want to extend me, implement myfunction(XXX arg), choose any type you want for XXX, but you have to choose one" So I don't want a concrete myfunction() that works for any type of argument but for only one, which is specified in the concrete Class.

Something like this:


I hope, I could make clear what I mean? Maybe somebody can give me a hint, I would be very grateful..

Have a nice day!
[ August 07, 2007: Message edited by: Jakob Kaiser ]
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the best that I know this is not possible. Due to polymorphism, when you override the base class method, it the arguments cannot be of a sub class as the base class method can be passed the generic type arguments.

One way to achieve this would be to check the type at run time and then process accordingly.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jakob,

Java 5 introduced generics, which does provide a way to do something like what you want. You can have a base class like



And then subclasses would pick a type for T:



Now, of course, you can't use these polymorphically.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!