Suppose we have a class Class , in which there is a method public void SendMessage { System.out.println("Blah!"); } public void SendMessage { System.out.println("Blah!"); } . We create objects A and B. Suppose you want object B to use some other implementation of the SendMessage method. Is it possible in Java to do this without creating for B separate class inheriting from Class ?

  • Such an option, how to pass an argument to the method with a string that should be output does not fit? - pavlofff
  • Is your question purely theoretical, or do you have a practical task that you would like to solve? If the task is practical, better give the original problem. It can probably be solved differently. - Tagir Valeev
  • Read more here - Tagir Valeev
  • @TagirValeev I write a chess engine, in which there are two types of positions: deliberated and real (on the board). Since there is only one real position, I would not like to create a separate class for the sake of one instance. - velikiyv4
  • I forgot to say, the real position needs to be recorded in each history after each move, so I would like to add an entry to the history to the function making the move. - velikiyv4

2 answers 2

Just override the method not in the heir, but directly when creating an instance of the class:

 public class Main { public static void main(String[] args) { SomeClass A = new SomeClass(); SomeClass B = new SomeClass(){ @Override public void sendMessage() { System.out.print("Babah!!"); } }; A.sendMessage(); B.sendMessage(); } } public class SomeClass { public void sendMessage (){ System.out.println("Blah!"); } } 

The result of the program:

Blah!
Babah !!

  • So you create a separate class in this case, it's just anonymous. B.getClass().equals(A.getClass()) returns false . And the number of loaded classes in the application will be more. Well, maybe the questioner will be happy with this decision, anyway there is no more honest option “without creating a class”. - Tagir Valeev
  • @TagirValeev Yes, it is. This is the only adequate solution that occurred to me. I find it hard to imagine where, in practice, a “clean” solution of the issue may be needed. Most likely, the author does not fully understand the concept of the PLO itself and tries to invent a “bicycle”. - pavlofff

Add an override to the SendMessage method.

If this option does not suit you, read this:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable .html

You can also read this article:

http://www.onjava.com/pub/a/onjava/2003/05/21/delegates.html?page=2

  • Probably not overloading (one method name, with different signature of arguments), but overriding (changing the actions performed in descendants / instances) - pavlofff
  • @pavlofff probably overload), public void sendMessage (int a) {sout ("Ah!");} - Mikhail Ketov
  • @ MikhailKetov Absolutely accurate - override, not overload. Detailed educational program on the issue . - pavlofff