Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Extended class access  RSS feed

 
Rafael Andrade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I got a simple question, that I thought would work, but it didn�t. I have 3 classes. Let�s say A, B and C. On class A I got the main. C extends B. I got a method on class B that I want to access in the main method at class A. Let�s see the example:

public class A {
public static void main(String[] args) {
B instance = new C();
// or C instance = new C();
instance.hello();
}
}

public class B {
String hello(){
return ("hello");
}
}

public class C extends B {}

I was really sure this would work, but didn�t. Since C extends B, once instantiated, it shoud access it�s methods, because it�s not private. But the code above gives no compilation error, neither shows any output. What�s wrong?

Thanks.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rafael Andrade:
...the code above gives no compilation error, neither shows any output. What�s wrong? ...

Simply returning a String reference from a method is not the same as printing a String. If you want output, you will need a System.out.println.
 
Rafael Andrade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You�re right, Marc, I didn�t see that!
Thanks for the quick answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!