• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing abstract classes

 
kavi ram
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
when i execute this code, it display java.lang.NoSuchMethodError:main error. if anybody know please help me.


Thanks in Advance.
abstract class Abs
{
private int i;
public abstract void first();
{
System.out.println("in first");

}

}
 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
what's wrong with that output..
there is no main method in your code.when executing it is searching for main method which is the gate way for java console programs. so it is giving runtime exception indicating that there is no main method.
i think your doubt cleared...
if not post your exact doubt..
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kavi ram:
hi all,
when i execute this code, it display java.lang.NoSuchMethodError:main error. if anybody know please help me.


Thanks in Advance.
abstract class Abs
{
private int i;
public abstract void first();
{
System.out.println("in first");

}

}



Do you really want to execute this code with out the main method or can you execute this without main method ? Think about this. You will get the answer yourself.

One more thing,



what is this ?

choices:
1.An instance initializer.
2.Body of abstract method.

Answer: Astract methods cannot have a body.
 
kavi ram
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thank to your reply.
But i want to execute this without main method.

help me.
 
Mani Venkata Kanth
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well,what is the output your expecting?
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannnot call the class directly without executeing the main method. The JVM looks for the main method of the class to execute it. If you dont want to use main then you need to provide concrete implementation of the class. Use the code below:
abstract class Abs{
private int i;
public abstract void first();
{
System.out.println("in first");

}

}
class Test extends Abs{


public void first(){


}
public static void main(String[] args) {
new Test();
}
}

It will execute the instance initializer block in you abstract class also.

Hope this helps...
 
kavi ram
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sharma.
 
Jay Suttiruttana
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note. The abstract class also needs to be extend and have its abstract method define before you can call on that class.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic