Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of main method in abstract class?

I know that we can write main method in abstract class, but what we can achieve from it ?

 public abstract class Sample
 {
         public static void main(String args[])
         {                        

            System.out.println("Abstract Class main method : ");

         }
 }

We can not create the object of abstract class ,so what is the use of main method in abstract class ?

like image 928
snehal Avatar asked Feb 23 '14 11:02

snehal


People also ask

How can we call Main method from abstract class?

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it. public abstract myMethod(); To use an abstract method, you need to inherit it by extending its class and provide implementation to it.

Why do we need a main method?

In any Java program, the main() method is the starting point from where compiler starts program execution. So, the compiler needs to call the main() method. If the main() is allowed to be non-static, then while calling the main() method JVM has to instantiate its class.

What is the use of main class?

The main() method can appear in any class that is part of an application, but if the application is a complex containing multiple files, it is common to create a separate class just for main(). The main class can have any name, although typically it will just be called "Main".

Can abstract class have public static void main?

Yes, abstract class can have Static Methods. The reason for this is Static methods do not work on the instance of the class, they are directly associated with the class itself.


3 Answers

Abstract just means you can't instantiate the class directly.

Loading a class is not the same as creating an instance of the class. And there's no need to create an instance of the class to call main(), because it's static. So there's no problem.

Abstract just means you can't instantiate the class directly. You can have constructors if you want - they might be needed for subclasses to initiate the object state. You can have static methods, including main() and they don't need an object so calling them is fine.

So you only got error when you try to create the object, which is when you run into the abstract limitation.

like image 154
Zeeshan Avatar answered Nov 15 '22 18:11

Zeeshan


public abstract class Abstrc
{
    Abstrc(){} // constructor
    public abstract void run(); // abstract method
    public static int mul(){return 3*2;} // static method
    public static void main(String[] args) 
    { // Static method that can be accessed without instantiation 
         System.out.println("Your abstract no is : " + Abstrc.mul());
    }
}

Your abstract no is : 6

like image 33
SDV Avatar answered Nov 15 '22 19:11

SDV


You can extend the abstract class and then the child class has a main method without specifying one there.

like image 23
Ulrich Thomas Gabor Avatar answered Nov 15 '22 17:11

Ulrich Thomas Gabor