0

Animal is a user defined class here.

Animal D = new Animal("Leo") {

        @Override public void makeNoise() {

              System.out.println("Roar!");

       }

};   D.makeNoise();
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Trent Boult
  • 117
  • 3

3 Answers3

4

Its called an anonymous class and used to define the class and any overridden methods at the same time.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • There is an extension of this for Single-Method-Interfaces now that is even more concise (probably does not apply here, but good to know if you use Java8). – Thilo Mar 02 '15 at 00:11
  • " define the class": Where "class" is a *subclass* of the class (or interface) named by `new`. So it does not instantiate `Animal` here, but an anonymous subclass instance. – Thilo Mar 02 '15 at 00:14
0

That is an anonymous class. For details about anonymous classes and why they are useful, see this tutorial on anonymous classes.

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59
0

This is used to override the initial

Animal.makeNoise()

method with a custom one for just this instance.