目录
Example of Anonymous Class in Java
Example #2
How will the files be generated for the Anonymous class?
Usage
Conclusion
首页 Java java教程 Java 中的匿名类

Java 中的匿名类

Aug 30, 2024 pm 04:01 PM
java

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1675
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能 PHP与Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP的影响:网络开发及以后 PHP的影响:网络开发及以后 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:许多网站的基础 PHP:许多网站的基础 Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP与Python:用例和应用程序 PHP与Python:用例和应用程序 Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles