Abstract class main() method

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


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. Umesh

    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. amit

    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. S. Nageswara Rao, Corporate Trainer Post author

      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. sanjay

    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. sanjay

        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. sanjay

            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. sanjay

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

  4. Prithu Yadav

    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

  5. Prithu Yadav

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

  6. Harish Raj

    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 Reply

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