• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Extended class, compilation error

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";
}
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic