Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


how can we use Abstract class with main()?


The question is can you have a main() method in the abstract class. 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.
 
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();
     }
}
 
 
 
 
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.
 

asked Sep 13, 2013 in Core java by rajesh
edited Sep 12, 2013
0 votes
22 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...