1
//Package in which I have both the Parent and the Child class
package learningJava;
public class MainMethodDemoParent 
{
  //Main method of the parent class
    public static void main(String... args)
    {
        System.out.print("This is the Parent class");
    }
}

//Child class which extends the Parent class
package learningJava;
public class MainMethodDemoChild extends MainMethodDemoParent

{

}

When I try to run the child class which is without the main method but is in the same package as that of the parent class so that I can access the main method of the parent class, in Eclipse there is no option to RUN the Child class a java Application. enter image description here

Amrit
  • 135
  • 1
  • 5
  • I hate to be that commenter, but there is *no* reason do this. Either run the parent method, or create another ```main``` in the child that does something else. – Michael Bianconi May 14 '20 at 19:48
  • I would not hate me if I were you and thanks for your comment but is there any way to overcome this in Eclipse? Through CMD you can compile the Parent class which has the child class as well in the same .java file. Hence there two .class files are created 1 for parent and another one for child. On executing the Child class you get the output. But how to achieve this in Eclipse? – Amrit May 14 '20 at 19:53

2 Answers2

1

I solved by doing this in eclipse : Step-1 Right click --> Run configurations

Step-2 Check the check box -Inclue inherited mains when searching for main class .

Step-3- Now hit Search again.you should be able to find the the child class you were looking for.

Check the second option

0

I can't see any reason why you might want to do that, however, if the purpose is to learn how to run methods of one class from another, you can do create a constructor for MainMethodDemoParent, print your message in there and then build an instance of that class in MainMethodDemoChild. Something like this:

class MainMethodDemoParent 
{
    public MainMethodDemoParent()
    {      
        System.out.print("This is the Parent class");
    }
}

//Child class which extends the Parent class MainMethodDemoChild
class MainMethodDemoChild
{
  public static void main(String[] args)
  {
    new MainMethodDemoParent();
  }
}

NOTE: You do not need to extend MainMethodDemoParent in MainMethodDemoChild if done this way. If for some reason you still want to extend, you can call method names directly if they are public, or use the super keyword super.methodName();