In code,
class MyObject {
public String doThing() {
return "doh";
}
}
class MyClass {
private myObject = null;
public MyClass() {
myObject = new MyObject() {
public String doThing() {
return "huh?";
}
};
}
What is it called when myObject is assigned a new object? I'm technically trying to find out if the 'doThing' overrides the method from MyObject, or if it redefines it, but I have no idea what to search on to find an answer - and no idea what question to ask without knowing what it's called when you create a new instance of an object on the fly and give it an implementation.
You are creating an anonymous inner class that is a subclass of MyObject
, so yes, you are overriding the doThing
method, if is that what you ask.
By the way, anonymous classes are like named classes, they have their own bytecode in their .class
file, that is named like their enclosing class suffixed with a dollar sign and an number.
If you want to experiment by yourself, you can use the method getClass()
of myObject
and extract information about it, like the name, parent, implemented interfaces, generic arguments, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With