Interview questions for local inner classes, anonymous inner classes, anonymous inner classes

local inner class

1. How to declare?
[modifier] class external class [extends parent class] [implements parent interface] {

[Modifier] return value type **method**name ([parameter list]){

 	[modifier] class inner class [ extends father] [ implemends parent interfaces]{
 	}
 }	

}

Description: Modifiers for local inner classes
(1) Permission modifier: none
(2) static does not
(3) abstract can have
(4) final can have

2. Use
(1) Local inner classes have scope
(2) Whether the non-static member variables of the outer class can be used in the local inner class depends on whether the method is static or not.
(3) The local inner class can use the local variable of the method, but the local variable must be declared final
If you have to manually add final before JDK1.8
After JDK1.8, the automatic addition of final means the default is final

3. Local inner classes also have bytecode files
outer class $number inner class

4. Members
A local inner class cannot have static members

public class TestLocalInner {

}

class Outer{
	private static int i;
	private int j;
	
	public void outTest() {
		int a = 10;// local variable
		//a = 20;
		class Inner{
			public void test() {
				System.out.println(i);
				System.out.println(j);
				System.out.println(a);
			}
		}
		
		Inner in = new Inner();
		in.test();
	}//Inner's scope can only go here
	
	public static void outMethod() {
		class Inner{
			public void test() {
				System.out.println(i);
//				System.out.println(j);//j cannot be used here because this method outMethod is a static method
			}
		}
	}
	
}

anonymous inner class

1. Declaration format
Location: where the object was created
Features: Create objects while declaring classes, anonymous inner classes only have unique objects

new parent class([parameter list]){
};

new interface (interface has no parameter list) {
};

2. Anonymous inner class is a special kind of local inner class. Any restrictions of local inner class, anonymous inner class has
(1) To use the local variables of the methods of the outer class in the anonymous inner class, you need to add final modification
(2) The use of non-static member variables of the outer inner class in the anonymous inner class is limited by the method in which it is located.
If the method is static, you cannot make a non-static member of the outer class

3. Bytecode
outer class $number.class

4. Members
cannot have static members

5. Anonymous inner classes have no names and no modifiers

public class TestNoNameInner {
	public static void main(String[] args) {
		Object obj = new Object();//This creates an object of the parent class
		System.out.println(obj.getClass());//runtime type
		
		//Anonymous inner class, which creates objects of subclasses
		Object in = new Object() {
			
		};
		System.out.println(in.getClass());//runtime type
		
		//Create object of anonymous inner class of MyClass subclass
		//polymorphic reference
		MyClass my = new MyClass() {

			@Override
			public void test() {
				// TODO Auto-generated method stub
				System.out.println("Subclass overrides abstract method of superclass");
			}
			
		};
		
		my.test();//Calling directly with my, the method of the parent class should be overridden for the subclass, and the method overridden in the subclass should be called directly
	
	
		MyClass my2 = new MyClass("parameterized construction") {

			@Override
			public void test() {
				// TODO Auto-generated method stub
				System.out.println("Subclass overrides abstract method of superclass");
			}
			
		};
		
		my2.test();
		
		
		//Create an anonymous inner class object of the implementation class of MyInter
		//polymorphic reference
		MyInter my3 = new MyInter() {
			@Override
			public void test() {
				// TODO Auto-generated method stub
				System.out.println("abstract methods that implement an interface");
			}		
		};
		my3.test();
		
	}
}


abstract class MyClass{
	private String info;
	public MyClass() {
		super();
	}
	public MyClass(String info) {
		super();
		this.info = info;
	}
	public abstract void test();
}


interface MyInter{
	void test();
}


interview questions

topic:
(1) Create a subclass of Object in the form of an anonymous inner class
(2) And declare a method in the subclass public void test(){}
print a sentence
(3) call this method

public class TestObject {
	public static void main(String[] args) {
		
/*		//Polymorphic reference, compile time, look at the parent class
		Object obj = new Object() {
			public void test() {
				System.out.println("XXX");
			}
		};
		
		//There is no way to force the conversion here, because the subclass is anonymous
		obj.test();*/
		
		//Anonymous object.method with anonymous inner class
		new Object() {
			public void test() {
				System.out.println("XXX");
			}
		}.test();
	}
}

Tags: Java Polymorphism abstract class

Posted by dominicd on Wed, 01 Jun 2022 10:42:06 +0530