首页 > Java > java教程 > Java 中的匿名类

Java 中的匿名类

WBOY
发布: 2024-08-30 16:01:31
原创
854 人浏览过

It is defining a class with no identity, i.e. a class without a name. They are used whenever you need to override the methods of a class, abstract class or interface. Anonymous inner class provides you with the blueprint, outline or template of the class but does not define or declare the behavior of the class. In this topic, we are going to learn about Anonymous Class in Java.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Let us look at some of the key concepts before proceeding.

One of the very important properties in an object-oriented language is Abstraction. Abstraction is the property that provides the blueprint/ template of methods, i.e. just declaration without defining them.

Java provides two such abstraction levels such as

  1. Interface
  2. abstract
  • The interface supports complete abstraction, and it does not allow to define any method in it.
  • Abstract classes support partial abstraction where a mixture of both abstract and normal method can reside.
  • A class extends the abstract class and implements an interface.
  • As abstraction is clear to you, we will look into one example where we have used interface or abstract class to write the anonymous inner class.

Example of Anonymous Class in Java

Following are the examples of anonymous class in java:

Example #1

Anonymous class for defining the method of an interface.

Code:

interface First
{
void printing();
}
public class Test
{
public static void main(String[] args)
{
First obj = new First()
{
public void printing() {
System.out.println("I am an inner class defining interface method");
}
};
obj.printing();
}
}
登录后复制

Output:

Java 中的匿名类

Explanation:

  • An interface is created naming First, and a method is declared as printing.
  • Remember all the methods in an interface are public and abstract
  • Next, we created a concrete class, and in the main method, we have to define the interface method by creating the interface object.
  • We cannot create an object of an interface or abstract class. But here, we are creating a class with no name and defining the method of that interface.
  • After creating an instance of an interface, we open the curly braces just like we do for class and define the printing function we have declared in the First interface.
  • At the end of the curly braces we have given after instance creation, we must provide asemicolon.
  • Such a block is known as the Anonymous inner class.
  • After the block, the first name as obj is used to call the function defined inside the anonymous inner class.
  • This is a situation where we are defining interfaces inside any method so-known as a local inner class.

Example #2

Anonymous class for defining the method of an abstract class.

Code:

abstract class AbstractClass {
abstract class AbstractClass {
public abstract void display();
}
public class Test {
public static void main(String[] args){ AbstractClass obj = new AbstractClass()
{
public void display() {
System.out.println("I am an inner class defining abstract class method.");
}
};
obj.display();
}
}
登录后复制

Output:

Java 中的匿名类

Explanation:

  • An abstract class is created naming AbstractClass, and an abstract method is declared as a display.
  • In an abstract class, the method named as abstract is declared a without abstract method can be defined.
  • Next, we created a concrete class, and in the main method, we have to define the abstract class method by creating its object.
  • After creating an abstract class instance, we open the curly braces just like we do for class and define the display function we have declared in AbstractClass.
  • We do not have to write the abstract keyword in front of the display method again as we have already declared it abstract in AbstractClass.
  • At the end of the curly braces we have given after instance creation, we must provide a semicolon.
  • After the block, the instance created for AbstractClass name as obj is used to call the function defined inside the anonymous inner class.
  • This is a situation where we are defining interfaces inside any method so-known as a local inner class.

How will the files be generated for the Anonymous class?

For the above Test class, a class file will be generated like normal named as Test.class.

For the anonymous inner class created, as there is no identity, a class file is still generated named as:

Test$1.class ---> anonymous class
登录后复制

If you create another anonymous inner class in the same class Test, then the class file will be named as:

Test$2.class
登录后复制

Some differences from normal classes:

  • Anonymous inner classes can implement only one interface at a time, whereas normal classes can implement manyinterfaces.
  • A normal class extends an abstract class and implements an interface at one time, but the anonymous inner class can only extend an abstract class or implement an interface at a single.
  • We cannot define any constructor inside the anonymous inner class due to a lack of class name/ identity.
  • We cannot have any static variables in an anonymous inner class as an inner class are associated with an instance.

Usage

Anonymous inner classes are mostly used in Graphic User Interface applications. When an object created has to be used only once, then the anonymous class can be used. When an object created has to be used only once, then the anonymous class can be used.

We can create an instance of an inner class object by first creating an object of outer class such as:

Outer class as Person so for creating an instance of an inner class inside person class

Person p = new Person();
Person.InnerClass innerClassObj = p.new InnerClass();
登录后复制

Conclusion

Anonymous inner class is a very important feature to know the power of object-oriented language and also is frequently asked questions in the technical interviews. Anonymous inner class gives you the liberty to define a class body method with no name.

以上是Java 中的匿名类的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板