Why java allows final class A {abstract class B {}}

In java, it is decided that a class with an abstract method will always be an abstract class (or interface). ** But a class with an abstract inner class can be a concrete class.

For example, the following code is executable.

java


final class A
{
    static abstract class B{String str="hoge";}
}

public class Main
{
    public static void main(String...args)
    {
        System.out.println(new A_B().str);
      //System.out.println(new A.B(){}.str); (Anonymous class)But it ’s good
    }
} 

class A_B extends A.B{}

If the concrete class doesn't have an abstract inner class

Although it is a seemingly contradictory assumption, consider the case where you do not want to recognize diversity in the macro (whole), but you want to recognize diversity in the micro (details).

Such assumptions are useful when considering the relationship between micro and macro, such as statistical mechanics.

For example, suppose you create a ʻAir (air) class and a Molecule` (numerator) class.

Molecules gather to create air, so you can say ʻAir has a Molecule [] `.

Various molecules such as hydrogen molecules, water molecules, and carbon dioxide molecules can be considered. Therefore, H2 extends Molecule, H2O extends Molecule, CO2 extends Molecule While inheriting and using it like this, there is no molecule called "mere molecule", so I don't think it will be used without inheriting it. So Molecule is likely to be an abstract class.

On the other hand, I don't want to make it an abstract class because it's hard to think of subordinate concepts in ʻAir`.

Here, if you want to define a method in ** Molecule that you only want to be called by ʻAir` ** what if?

Usually it can be solved by making Molecule the inner class of ʻAir. If the concrete class ʻAir doesn't have the abstract class Molecule inside, you'll probably have to do the following tedious things:

  1. Have final public static class $ {private $ () {}} in ʻAir`
  2. Give the Something method a ʻAir. $` Type argument

Realize with.

Since the ʻAir. $Type ispublic static and can be seen from other than ʻAir, it can be used as a dummy argument type for methods of classes other than ʻAir. However, since its constructor is private, instantiation can only be done from ʻAir. Other than ʻAir, that is, ʻAir. $ Type actual arguments cannot be passed.

Thanks to that, "if a method has a ʻAir. $ Type as a formal argument, that method can only be called from the ʻAir class" is established.

An example is shown below.

python


public class Test2 {

	public static void main(String[] args) 
	{

        //new H2O.only_for_air(new Air.$());Is an access permission error. In other words, only from other than Air_for_Can't call air
		new Air().molecule_caller(new H2O());//Can be called from Air

	}

}



class Air
{
	final public static class ${private $(){}}//Finalized because it would be a problem even if it was rewritten in the child class of Air
	
	void molecule_caller(Molecule m)
	{
		new H2O().only_for_air(new Air.$());
	}
	
	
}

abstract class Molecule
{
	void only_for_air(Air.$ $)
	{
		System.out.println("succeeded");
	}
}


class H2O extends Molecule{}

Well, I have to do such a troublesome thing.

So I wondered if "a concrete class with an abstract inner class" would be possible in java.

Recommended Posts

Why java allows final class A {abstract class B {}}
java (abstract class)
[java] abstract class
Why does Java call a file a class?
About java abstract class
Java learning memo (abstract class)
Why implement an abstract class (Abstract)
Why are class variables needed? [Java]
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
How to convert a solidity contract to a Java contract class
A quick review of Java learned in class
Java class methods
java Scanner class
Java HashMap class
[Java] Nested class
Java anonymous class
About Java class
[Java] Object class
Java local class
java final modifier
Write a class that can be ordered in Java
Write a class in Kotlin and call it in Java
A quick review of Java learned in class part3
Use of Abstract Class and Interface properly in Java