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
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.