This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top Level Class doubt

 
Sarang Khankhojr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is exactly a Top level Class? As per my understanding
the class after which java file is named.
I tried this:
abstract class myclass
{
public void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
I named my file as myclass.java
Program compiles fine.But at runtime doesn't find main method?
Can someone explain why? And if that is true how can abstract class be top level class?
 
Sandra Marti
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarang,
You have errors in your program. It should give you errors while compiling. Following is the corrected version:
abstract class myclass
{
public abstract void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
The highlighted ones are the ones you had missed. The program runs fine with output 'just a test'.
A top-level class is a class that contains a nested class but is not itself a nested class.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarang:
What is exactly a Top level Class? As per my understanding
the class after which java file is named.
I tried this:
abstract class myclass
{
public void amethod();
public static void main(String[] args)
{
System.out.println("Just a test");
}
}
class1 extends myclass
{
public void amethod()
{
System.out.println("I am in subclass");
}
}
I named my file as myclass.java
Program compiles fine.But at runtime doesn't find main method?
Can someone explain why? And if that is true how can abstract class be top level class?

Top Level class is any class which can be instanciated, it doesn't necessarily have to be the one that is the file name. You can have multiple classes defined in a single source file, however only one can be public. This also means that you can have multiple top level classes and multiple abstract classes.
Save the following in a file called myclass.java and compile it

then run class1 and class2 and see the results.
class1 and class2 are examples of a Top Level Class, you can create n object with these. myclass and myclass2 are not top level classes since they are absract and you can not create an object directly.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sarang,
the work of the JVM lies in trying to find the main method in applications and executing it. This main method executes without a instance of the class as it is static. As long as u do not do anything that is not allowed in the main method everything goes well. In the code if u had tried to instansitate an object of the abstract class then the compiler would be screaming. However since u have only printed out a statement to the console everything goes off well
Refer JLS 2 section 7.6 for Top level classes more details.

Regds
Rahul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic