• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extended class access

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You�re right, Marc, I didn�t see that!
Thanks for the quick answer.
reply
    Bookmark Topic Watch Topic
  • New Topic