aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Extended class, compilation error " Watch "Extended class, compilation error " New topic
Author

Extended class, compilation error

Ram Han
Ranch Hand

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


// in testB.java
class testB extends testA{
public void mytest2(){
System.out.println(mytest());
}

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

}
}

What is the result of compiling the class testB.

It is a compilation error , How it is possible ?


SCJP,SCWCD,IBM 285,MCTS
Joe LeGrand
Greenhorn

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
Sheriff

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 TestA.java...

You would compile with javac TestA.java, 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
sscce.org
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
if file saved as testB.java
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%)
 
wood burning stoves
 
subject: Extended class, compilation error