Reputation: 131
I don't know what this way of passing an object as a parameter is called. So I didn't know what to search for on the site. My apologies if this has been asked before, which I'm sure it has.
Can somebody tell me what the difference is between these two ways of passing on object as a parameter? And why can't I override properties as well as methods in the first way?
myObject.doSomethingWithOtherObject((new OtherObject() {
// why can't I override properties here?
public String toString () {
return "I am the other object.";
}
...
}));
myOtherObject = new OtherObject();
myObject.doSomethingWithOtherObject(myOtherObject);
Then I have a second question, but maybe answering the first question will also answer this one. Why does this not work:
public class OtherObject {
public OtherObject (String str) {
super();
}
}
myObject.doSomethingWithOtherObject((new OtherObject("string arg") {
public void overrideSomething () {
}
}));
The string "string arg" is not passed to the constructor as I would have expected. Instead in complains about not finding the constructor OtherObject(). Why does Java not recognize I'm trying to send an argument to the constructor?
Thanks for reading!
Upvotes: 0
Views: 134
Reputation: 723
First of all I'd like to confirm what my previous speakers said:
When you add curly brackets to the Object initialization you'll create an anonymous subtype of the class for which you can override methods as you did it in your example.
But now to your comment-question: why can't I override properties here?
You can: by adding another pair of curly brackets, which will create a constructor for the anonymous type, e.g.:
public class OtherObject {
protected String name;
public OtherObject(String name) {
super();
this.name = name;
}
public String toString() {
return getClass() + ": " + name;
}
public static void main(String[] args) {
OtherObject o1 = new OtherObject("bar");
OtherObject o2 = new OtherObject("bar") { // will call parent constructor first -> name = "bar"
{
// constructor for the anonymous type
name = "foo"; // overwrite the name
}
};
System.out.println(o1); // prints "class OtherObject: bar"
System.out.println(o2); // prints "class OtherObject$1: foo"
}
}
Upvotes: 1
Reputation: 9559
Your code:
myObject.doSomethingWithOtherObject((new OtherObject() {
// why can't I override properties here?
public String toString () {
return "I am the other object.";
}
...
}));
Is roughly equivalent to:
class OtherSubObject extends OtherObject {
public String toString () {
return "I am the other object.";
}
}
...
myObject.doSomethingWithOtherObject(new OtherSubObject());
except that the subclass of OtherObject has been created without a name (i.e. is anonymous) and so cannot be referred to anywhere else.
Your code:
myOtherObject = new OtherObject();
myObject.doSomethingWithOtherObject(myOtherObject);
does not create a new class, it just passes an instance of OtherObject as the parameter.
Upvotes: 1
Reputation: 744
Calling a method with a new object which is followed by curly braces is like extending that class and it is called anonymous class in java.
myObject.doSomethingWithOtherObject(new OtherObject() {
public String toString () {
return "I am the other object.";
}
});
The reason you cannot override properties is probably because of their access modifiers. For overriding a class member it should have access modifier of at least protected.
public class OtherObject {
protected String name;
}
You can now override name in your anonymous class.
Upvotes: 0
Reputation: 1959
The first one is called an anonymous class. It means that you actually instantiate a subclass without a name, in order to override something.
In the second one, the compiler complains that you do not have a constructor. As described in the answer to your first question, you are actually subclassing here, meaning you have to define a constructor if you need one.
Upvotes: 1
Reputation: 14328
when you do new OtherObject
and then open curly brackets { ...
you are creating a new (annonymous) class. not a new Object of the already defined OtherObject class. have a look at Java's annonymous classes to better understand
Upvotes: 3