If there is main method in the program, but not in the main class of the program, can the program runs or not?

Ans: program will compile but not run i.e. runtime error.


Program:

// define a class one in which no main method will define.
class One
{
    // define toString method.
    public String toString()
    {
        return "hello toString method";
    }
    // define display method
    void Display()
    {
        // define print function
        System.out.println("hi display");
    }
}
//define another class B that extends the class One
class B extends One
{
    //define main method in class B
    public static void main(String args[])
    {
        //create a constructor of class one
        One a=new One();
        // call print function
        System.out.println(a);
    }
}



Sample Output:


Post a Comment

Previous Post Next Post