• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class

 
chetna_jain
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the doubt can abstract class have main method.
acc. to me as it cannot be insatinated it cannot have main method
thanx in advance
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes can have main method. Main methods have nothing to do with instantiation. Main method is used for executing your class.It is the entry point to your program.
The following abstract method has a main method which when executed outputs Hello.
Hope this helps
Roopa.
public abstract class Abs
{
public static void main(String [] args)
{
System.out.println("Hello");
}
abstract void methodA();

}
Originally posted by chetna_jain:
i have the doubt can abstract class have main method.
acc. to me as it cannot be insatinated it cannot have main method
thanx in advance

 
chetna_jain
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot roop i got my doubt cleared
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides,
main is static method and belongs to the class and not to an instance, no reason why it can't be in a an abstarct class. Moreover, you can have any other (user defined) static method in an abstract class, which can be invoked w/o creating an instance of a class.
HTH,
- Manish
 
Hassan Naqvi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good addition Manish.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class can even have a constructor
Ex: java.awt.Component
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class is declared 'abstract' it just means that not all of its methods are implemented. It can't be instantiated because it's not complete.
The 'abstract' tells you that if you want to use the class, you'll first need to subclass it and implement any abstract methods.
It may be a complete class with just one non-implemented method! Which means it can have full methods, ctors, and a main().
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

[This message has been edited by Jane Griscti (edited October 02, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic