Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a method in an instantiated Java object

I would like to override a method in an object that's handed to me by a factory that I have little control over.

My specific problem is that I want to override the getInputStream and getOutputStream of a Socket object to perform wire logging.

The generic problem is as follows:

public class Foo {     public Bar doBar() {         // Some activity     } } 

Where I'd like to take an instantiated Foo and replace the doBar with my own that would work as follows:

Bar doBar() {     // My own activity     return original.doBar(); } 

For the Socket I'm going to return an InputStream and OutputStream that are wrapped by logging to intercept the data.

like image 263
Tyler Szabo Avatar asked Oct 06 '10 18:10

Tyler Szabo


People also ask

Can we override object method in Java?

Because of this, all Java classes inherit methods from Object . Half of these methods are final and cannot be overridden. However, the other methods in Object can be and are overridden, often incorrectly. This article explains why it's important to implement these methods correctly and then explains how to do so.

How do you override an object in Java?

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.

What are the rules for overriding a method in Java?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.

Can you override an instance method?

3) An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.


2 Answers

Since Java uses class-based OO, this is impossible. What you can do is use the decorator pattern, i.e. write a wrapper for the object that returns the wrapped streams.

like image 154
Michael Borgwardt Avatar answered Sep 24 '22 01:09

Michael Borgwardt


I think there is a way to achieve the effect you want. I saw it orriginally used in swing with buttons to allow the programmer to make the button do something when it is clicked.

Say you have your Foo class:

public class Foo {   public Bar doBar() {     // Some activity   } } 

Then you have a runner class or something similar. You can override the doBar() method at the point of instantiation and it will only affect that specific object.

that class may look like this:

public class FooInstance{   Foo F1 = new Foo(){     public Bar doBar(){       //new activity     }   }    Foo F2 = new Foo();    F1.doBar(); //does the new activity   F2.doBar(); //does the original activity found in the class } 

I'm not entirely sure that will do the trick for you but maybe it'll set you in the right direction. If nothing else it is possible to override a method outside of the class, maybe that will help you.

like image 30
John Avatar answered Sep 23 '22 01:09

John