I have seen this doing many time but I am bit confused whether or not this would be called as anonymous class?
public class Test {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
System.out.println("##");
}
}.start();
}
}
Reason I am confused is because anonymous classes doesn't have name, but this clearly we already have a "Thread" class in Java API, so this means this has a name, and if it has a name then how it can be a anonymous class, and if it is not a anonymous class then what it is.
I know it is kind of silly but I am not able to reason myself deterministic-ally because I see valid arguments on both side.
Also, in above I can clearly override the run
method of Thread
class, now if I create my own class let's say MyClass
, define some methods in it and then try to do same thing as above then why I cannot override methods of MyClass
, they way I was able to override run
method of Thread
class.
public class MyClass {
private void myMethod1() {
}
private void myMethod2() {
}
}
public class Test {
public static void main(String[] args) {
new MyClass(){
// why I cannot override "myMethod1" and "myMethod1" of `MyClass`, they way I was able to override `run` method of `Thread` class
};
}
}