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();
 }
}


Abstract class main()
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?

27 thoughts on “Abstract class main() method”

  1. Sir,

    I have 4 years of experience only in manual testing. I have joined Java and Selenium Course it is helpful to my career. Please let me know about this.

    Thanks,
    Umesh

  2. Hi Sir,

    I have 3 years of experience in manual testing and now I want to change my skills from testing to Java.
    Please provide your suggestion how to do.

    1. To change, first your employer may not accept as he will not loose a programmer of 3 years experience in testing. You must show a strong reason for shifting. My advice is pass Java Certified Programmer and Java Web component developer exams. Then keep the certificates before your employer and express you are interested in Java programming. Request them to shift and the request should go on whenever opportunity comes.

  3. sir , i m fresher and now i’m working in IT department of company .it is possible to shift from networking to coding after experience.

      1. my interest in software development ,i do this job for because something is better than nothing .. means i m searching coding job last 2-3 months but did not find any job of software becoz of this i joined this job sir
        plz suggest sir and guide him what i do next

          1. sir ,i m doing IT Executive job in company my work related to network ,hardware ,backup,installation .
            i m completed my b.tech IT in 2013

          2. sir can i shift networking to coding or development job after one year or more experience , its possible or not ??

  4. Nice website….got some fair knowledge in Java after started using this website. Thank you so much Mr. Nageswara Rao.

  5. An abstract method cannot be declared as static. static method should contains the method body.If you declare any method as abstract & static then it will generates compile time error:- Illegal combination of modifiers : abstract & static

  6. Yes we can have all the concrete methods in abstract class without even a single abstract method. Try this code.
    eg:-
    public abstract class A
    {
    public void show1()
    {
    System.out.println(“hello from show1”);
    }
    public void show2()
    {
    System.out.println(“hello from show2”);
    }
    }
    class B extends A
    {
    public static void main(String args[])
    {
    B ob=new B();
    ob.show1();
    ob.show2();
    }
    }

  7. Hello Sir,

    Under what circumstance we go for this option? Can you give me a Real Time example for this?

    Thanks,
    Harish Raj

Leave a Comment

Your email address will not be published. Required fields are marked *