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{}
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:
final public static class $ {private $ () {}}
in ʻAir`Something
method a ʻAir. $` Type argumentRealize with.
Since the ʻAir. $Type is
public 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