Anonymous Class
Start course
2h 28m

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. If you remember, we talked about nested classes in our previous lessons. In this context, we examined static nested classes and non-static, that is, inner classes. We also examined local classes from inner classes but I said I would explain the anonymous classes after we learned about interface. Now that we have learned about the interface, we can examine anonymous classes. So, what is anonymous class?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except they do not have a name. This is important because the anonymous classes do not have a name. We use them if you need to use a local class only once. In this case, we can achieve our goal directly with the anonymous class without the need for subclass. Let's look at the example on this slide. We have an interface called Car and this interface has two constant variables: NAME and SPEED. And one method named canGo(). We can call this interface inside the main method without using any subclasses. Just like creating a new object from a class, when we use the new keyword, we can use the methods and variables in this interface and specify what the method should do. Notice that a curly parenthesis has been opened with a new keyword and a semicolon has been added to the end of this curly parenthesis. 

In fact, this is the best proof that anonymous classes are expressions. The syntax of an anonymous class expression is like the invocation of a constructor except that there is a class definition contained in a block of code. The anonymous class expression consists of the following: the new operator, the name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface Car. Parentheses that contain the arguments to a constructor just like a normal class instance creation expression. Note, when you implement an interface, like on this slide, there is no constructor so you can use an empty pair of parentheses. As in this example, a body which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not. Now, let's look at what anonymous classes can do or are limited by. An anonymous class has access to the members of its enclosing class. 

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. Like a nested class, a declaration of a type such as a variable in an anonymous class, shadow any other declarations in the enclosing scope that have the same name. We cannot declare static initializers or member interfaces in an anonymous class. An anonymous class can have static members provided that they are constant variables. We can declare the following in anonymous classes: fields, extra methods, instance initializers, and local class. But, we cannot declare constructors in an anonymous class. Okay, after this information about the anonymous class, let's go to the Eclipse and get some practice. First, I will create an interface. So, I will right click on the source folder in the object-oriented programming project and select the new interface option. The package name can be anonymousexample and the name of the interface will be Car and click the finish button.

Now, I will create two variables. The first is the string name. If you remember, the variables in the interface were constant. For this reason, I write the variable name as the uppercase letter and we need to assign a value to the variable in the interface because they are final by default. So, the value can be Ferrari. The other variable will be int SPEED and its value can be 300. And I will create a method, void canGo(). So, the methods in the interface don't have a body. Okay. Now, the interface is ready. First of all, we will implement the properties in the interface to the subclass without using the anonymous class and we will do our operations over the subclass. Later, we will do similar operations using the anonymous class without using sub classes. So, you will see the difference. Let's continue now. First, I'll create a subclass that implements these members. So, I right click on the anonymous example package and select the new class options. 

The class name can be Vehicle and click 'Finish'. Now, I will implement the car interface. After the implements keyword, I write 'Car'. And as you can see, we get a compilation error because there is a method we have to implement. So, I click the 'Add unimplemented methods' option and the Eclipse overrides the canGo method automatically. Now, in this method, I print a message to the console. System.out.println(NAME);. We can access the constant of the interface. After the name, I write 'can  go at' and after the plus sign, I write 'SPEED'. Yes, now let's create the test class. So, I right click on the anonymousexample package and select the new class options. The class name can be Test and I will check the check box for the main method and click 'Finish'. Now, I will create an object from the Vehicle class. Vehicle vehicle = new Vehicle();. 

Now, using the Vehicle object, I will call the canGo method vehicle.canGo(). Now, let's run the code and see. As you can see, the 'Ferrari can go at 300' message is printed on the console. Now, let's use the anonymous class now. I will convert these lines to the comment line. Now, without using the subclass, I will call the interface. Car myCar = new Car() and as you can see, we get a compilation error. It says, 'Cannot instantiate the type of car.' This is normal because we cannot instantiate the interface and abstract classes. Now, I will put curly braces after the car. The error still continues, but this time it says a different thing. The type of new Car(){} must implement the inherited abstract method, car.canGo(). 

So, I will click the 'Add unimplemented methods' option. And as you can see, the Eclipse creates a new block. So, this is named the anonymous class in Java. There is no class name. And last, we need to put a semicolon at the end of the block. Okay. Now, we can write some code in this canGo() method. I'll copy the print method in the vehicle class and I'll paste it in the canGo() method in the anonymous class. Okay. Last, to execute these codes, we need to call the canGo() method using the myCar object. So, here I write myCar.canGo();. Now, let's run and see. As you can see, we saw the same result. Excellent. Yes, this is how the anonymous class is used. Let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics