Will this implementation be called an anonymous class?

I've seen this for a long time, but I'm a little confused, will this be caused by the anonymous class?

public class Test {
    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                System.out.println("##");
            }
        }.start();
    }
}

      

The reason I am confused is because anonymous classes do not have a name, but this is obviously we already have a "Thread" class in the Java API, so this means that this has a name and if it does name, then how can it be an anonymous class, and if it is not an anonymous class, then what is it.

I know this is stupid, but I cannot reason myself deterministically, because I see valid arguments from both sides.

Also, in the above I can explicitly override the run

class method Thread

, now if I create my own class, say MyClass

define some methods in it and then try to do the same as above, then why can't I override the methods MyClass

, so how did I manage to override the run

class method Thread

.

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
        };
    }
}

      

+3


source to share


2 answers


  • Yes, your first class is anonymous since it is inline and extends from Thread. This is not the same as your Thread class.
  • Relatively MyClass

    : Of course, you cannot propagate private methods. This has nothing to do with anonymous classes and is the basic rules of Java inheritance. Make them public and you can expand them in a row.

To clarify, if you have a nested class that has a name, you can declare a variable of your own type:

public class Test {

    private static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("Foo");
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
    }
}

      

But if you have an anonymous class, you cannot do this:

public static void main(String[] args) {
    new Thread(){
        @Override
        public void run() {
            System.out.println("##");
        }
    }.start();


    // how can you declare a variable of the above type *with* its behavior?
}

      




Side bit: you should almost never extend Thread and instead run Runnable or even better use executors to help you stream.




re:

Ok, thanks for your answer, correct me in my understanding - using an anonymous class, I can create a completely new class (basically an interface implementation) at runtime, also I can extend an existing class and it will still be called as "anonymous class "?

Yes, you do create an entirely new class, but you don't necessarily implement the interface. Actually your example above has nothing to do with interfaces, and it all has to do with extending an existing concrete class. You can and often create anonymous classes that implement interfaces, with Runnable being a common example, but your example above is not "basically an interface implementation".

+5


source


public class Thread extends Object implements Runnable



So you are basically creating an anonymous class of a class Thread

, but because Thread

Class implements Runnable

(which is a functional interface that has one abstract method) -> you have a method @override run()

that comes fromRunnable Interface

+2


source







All Articles