• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Top Level Class doubt

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 coderanch. 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic