• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extended class, compilation error

 
Ram Han
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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 ?
 
Joe LeGrand
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic