Java anonymous class

What is an anonymous class?

A class in which a class definition and instantiation are described in a single expression without specifying a class name. Interface implementation / method retrieval, class inheritance / override / method retrieval can be performed without specifying a class name, which is convenient when you want to use a method only on the spot.

interface Inter{
	void methodA();
}

class SuperT{
	void methodS() {
		System.out.println("Super");
	}
}

class OuterT{ //Outer class
	void method1() {
		new Inter() { //Implement and instantiate the interface at the same time
			public void methodA() {
				System.out.println("methodA implementation");
			}
		}.methodA(); //Call methodA from the instantiated one
	}

	/*void method2() {
		new SuperT() {
			void methodS() {
				System.out.println("Override");
			}
		}.methodS();
	}*/

}

public class Tokumei {

	public static void main(String[] args) {
		OuterT ot = new OuterT();
		ot.method1();
		//ot.method2();
		
		System.out.println("-----When called after assigning to a SuperT type variable-----");
		SuperT st = new SuperT() { //Assign to a superclass type variable
			void methodS() {
				System.out.println("Override");
			}
			void methodSub() { //Anonymous subclass proprietary method
				System.out.println("Sub");
			}
		};

		st.methodS();
		//st.methodSub(); //You cannot call methods unique to anonymous subclasses with superclass variables
		
		System.out.println("-----When called directly from an object-----");
		new SuperT() {
			void methodS() {
				System.out.println("Override");
			}
			void methodSub() {
				System.out.println("Sub");
			}
		}.methodSub(); //Can be called directly
		}
}

Output result

methodA implementation
-----When called after assigning to a SuperT type variable-----
Override
-----When called directly from an object-----
Sub

point

(1) When inheriting a class, anonymous subclasses can override methods.

(2) When calling after assigning an anonymous subclass that inherits a superclass to a variable of superclass type, only the methods (including overridden ones) in the superclass can be called. (The mechanism is the same as for normal superclasses and subclasses, and the accessible range changes depending on the variable type.)

(3) When calling directly from an object, the method set independently in the anonymous subclass can also be called.

Recommended Posts

Java anonymous class
Anonymous class (anonymous class)
Java class methods
[Java] Class inheritance
java Scanner class
Java HashMap class
java (abstract class)
[Java] Nested class
About Java class
[java] abstract class
[Java] Object class
Java local class
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
[Java] About anonymous classes
Java inner class review
Java class type field
Java programming (class method)
About Java String class
Java programming (class structure)
About java abstract class
Java Callback Simple Sample Part2 Anonymous Class Example
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java inflexible String class substring
[Implementation] Java Process class notes
About Java class loader types
How to use java class
[Java] Comparator of Collection class
Java
Java learning memo (abstract class)
Summary of Java Math class
[Java] What is class inheritance?
About Java class variables class methods
Java
[Java basics] What is Class?
Implement Thread in Java and try using anonymous class, lambda
Create an immutable class with JAVA
Why are class variables needed? [Java]
Call Kotlin's sealed class from Java
Various methods of Java String class
[Java] Memo for naming class names
How to disassemble Java class files
Kotlin Class to send to Java developers
StringBuffer and StringBuilder Class in Java
How to decompile java class files
[Java] How to use LinkedHashMap class
java (use class type for field)
Java source code reading java.lang.Math class
[Java] Button template class by Graphics
How to use class methods [Java]
[Java] How to use Math class
Java learning (0)
Studying Java ―― 3
[Java] array
What is a class in Java language (3 /?)
Java protected
[Java] Annotation
Java array
Kotlin Class part.2 to send to Java developers