Way2Java

Abstract class main() method

Can you have Abstract class main() method?

Yes, definitely, because main() is a concrete method and abstract class allows concrete methods. But what you can you do with the main() when you are not allowed to create objects of abstract classes. But, you can create objects of another class and use other class methods by composition.

Following program illustrates on Abstract class main()
class Test
{
  int x = 10;
  public void display()
  {
    System.out.println("Hello 1");
  }
}
public abstract class Demo
{
 public static void main(String args[])
 {                        
   Test t1 = new Test();
   System.out.println("From abstract class main(): " + t1.x);
   t1.display();
 }
}



Output screen on Abstract class main()

Observe, Demo is declared abstract and contains main() method. In the main() method, object of Test class t1 is created and the members of Test are called.

Other interested topics on abstract classes

1. Can you have a constructor in abstract class?
2. Can you have all concrete methods in an abstract class without even one abstract method as in the above Demo class?
3. Can an interface have constructor?
4. Can abstract method be static?
5. Why the methods of an interface should be overridden as public only?