The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Extended class, compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extended class, compilation error " Watch "Extended class, compilation error " New topic

Extended class, compilation error

Ram Han
Ranch Hand

Joined: Feb 26, 2002
Posts: 49
// in
public class testA{
public void mytest(){
System.out.println(" I am in Superclass");

// in
class testB extends testA{
public void mytest2(){

public static void main ( String a []){
testB b=new testB();


What is the result of compiling the class testB.

It is a compilation error , How it is possible ?

Joe LeGrand

Joined: Dec 02, 2005
Posts: 6
Your method myTest() in TestA is void and don't return anything.

You can't do in myTest2 : System.out.println(mytest());

You can replace your myTest() by this one and all will be fine
public String mytest(){
return "I am in Superclass";
achayya matta
Ranch Hand

Joined: Oct 27, 2005
Posts: 111
hai murali,
The main thing is when u have a public Access modifier infron of a class then u must write the "main()" method in that class only.

hope it helps u..
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ashok reddy devaram:
... The main thing is when u have a public Access modifier infron of a class then u must write the "main()" method in that class only...

Well, if you have a top-level class or interface that's declared public in a Java file, then the file must share that same name, so you would compile using that file name.

However, any class can contain a main method. And once you have your class files compiled, you can run whichever you choose.

For example, the following can be saved in a single file called

You would compile with javac, because that's the name of the file. But both of these classes have main methods, so once compiled, you can run either java TestA or java TestB.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
if file saved as
remove public from testA
write super.mytest() and comment Sys.out.prin(mytest());

as this function takes String as arguments but here its void

now it compiles fine.

Harish<br />SCJP 1.4 (85%)
I agree. Here's the link:
subject: Extended class, compilation error
It's not a secret anymore!