Difference between Anonymous Inner Class and Lambda Expression | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Anonymous Inner Class

Anonymous Inner Class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.

package com.codeFactory;

/**
 * @author code.factory
 *
 */
public class AnonymousClass {
	public static void main(String... args) {
		Name name = new Name() {
			
			@Override
			public void getName() {
				System.out.println("Name : " + name);
			}
		};
		name.getName();
	}
}

interface Name {
	String name = "Code Factory";
	void getName();
}

Lambda Expressions

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.

lambda expressions are added in Java 8 and provide below functionalities.

  • Enable to treat functionality as a method argument, or code as data.
  • A function that can be created without belonging to any class.
  • A lambda expression can be passed around as if it was an object and executed on demand.
package com.codeFactory;

/**
 * @author code.factory
 *
 */
public class LambdaExpressions {

	public static void main(String... args) {
		Name name = (x) -> System.out.println("Name : " + x);
		name.getName("Code Factory");
	}
}

interface Name {
	void getName(String name);
}

Difference :

Anonymous Inner ClassLambda Expression
It is class without nameIt is method without name. (Anonymous function)
It can extend abstract and concrete classIt can not extend abstract and concrete class
It can implement an interface that contains any number of abstract methodsIt can implement an interface which contains a single abstract methods
Inside this we can declare instance variablesIt does not allow declaration of instance variables, whether the variables declared simply act as local variables
Anonymous inner class can be instantiatedLambda expression can not be instantiated
Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer objectInside Lambda expression, “this” always refers to current outer class object that is, enclosing class object
It is the best choice if we want to handle multiple methodsIt is the best choice if we want to handle interface
At the time of compilation, a separate .class file will be generatedAt the time of compilation, no separate .class file will be generated. It simply convert it into private method outer class
Memory allocation is on demand, whenever we are creating an objectIt resides in a permanent memory of JVM

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s