How is it possible to create a sub class object within it's base class?

后端 未结 5 1625
自闭症患者
自闭症患者 2021-01-25 17:38
class arijit
{
 public static void main(String args[])
 {
  System.out.println(\"Base class main\");
  amit ab=new amit(); //how is it possible as the sub class object a         


        
5条回答
  •  礼貌的吻别
    2021-01-25 18:29

    The crux of your question seems to be that you're creating an instance of the amit class within the main method of its base class, arijit, and you're wondering how that's possible.

    Why wouldn't it be possible? Your main method references the subclass by name, so it's just like any other class from that point of view. You can do it in non-static members, too, if you like.

    Architecturally, it usually indicates that there's a problem with your structure if the base class knows the intimate details (like the names) of its subclasses; that's not the usual way 'round of things.

提交回复
热议问题