Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Anonymous Classes

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Anonymous Class

      Java Anonymous Class

      An anonymous class in Java is an inner class which is declared without any class name at all. In other words, a nameless inner class in Java is called an anonymous inner class. Since it does not have a name, it cannot have a constructor because we know that a constructor name is the same as the class name.

      Use of Java Anonymous Inner Classes

      Anonymous inner classes are used when you want to create a simple class that is needed for one time only for a specific purpose. For example, implementing an interface or extending a class.

      Defining Anonymous Class in Java

      You can define an anonymous inner class and create its object using the new operator at the same time in one step.

      Syntax

      The syntax of anonymous nested class is as follows
      new(argument-list){
      // Anonymous class body
      }

      Types of Anonymous Inner Classes in Java

      • Anonymous inner class that extends a class
      • Anonymous inner class that implements an interface
      • Anonymous inner class as an argument

      1. Anonymous inner class that extends a class

      You can use an anonymous inner class to extend a class in Java.

      Example: Anonymous inner class that extends a class

      In the following example,we're using a new keyword to create an object of the anonymous inner class that has a reference of parent class type.
      package com.tutorialspoint;
      
      class Car {
      public void engineType() {
      System.out.println("Turbo Engine");
      }
      }
      public class Tester {
      public static void main(String args[]) {
      Car c1 = new Car();
      c1.engineType();
      Car c2 = new Car() {
      @Override
      public void engineType() {
      System.out.println("V2 Engine");
      }
      };
      c2.engineType();
      }
      }
      Output
      If you compile and execute the above program, you will get the following result
      Turbo Engine
      V2 Engine

      2. Anonymous inner class that implements an interface

      You can use an anonymous inner class to implement an interface in Java.

      Example: Anonymous inner class that implements an interface

      In the following example,we're using a new keyword to create an object of the anonymous inner class that has a reference of an interface type.
      package com.tutorialspoint;
      
      interface Software {
      public void develop();
      }
      public class Tester {
      public static void main(String args[]) {
      Software s = new Software() {
      @Override
      public void develop() {
      System.out.println("Software Developed in Java");
      }
      };
      s.develop();
      System.out.println(s.getClass().getName());
      }
      }
      Output
      If you compile and execute the above program, you will get the following result
      Software Developed in Java
      com.tutorialspoint.Tester$1

      3. Anonymous inner class as an argument

      We can use the anonymous inner class as an argument so that it can be passed to methods or constructors.

      Example: Anonymous inner class as an argument

      In the following example,we're passing an anonymous inner class as an argument to one method.
      package com.tutorialspoint;
      
      abstract class Engine {
      public abstract void engineType();
      }
      class Vehicle {
      public void transport(Engine e) {
      e.engineType();
      }
      }
      public class Tester {
      public static void main(String args[]) {
      Vehicle v = new Vehicle();
      v.transport(new Engine() {
      @Override
      public void engineType() {
      System.out.println("Turbo Engine");
      }
      });
      }
      }
      Output
      If you compile and execute the above program, you will get the following result −
      Turbo Engine