• 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

Calling a superclass method by void return type

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code. It works fine. My confusion is, methods callToAClassprintAndReturnSomething() and callToBClassprintAndReturnSomething() , both have void return type as void. But methods being called in them printAndReturnSomething() and printAndReturnSomething() both have return types of objects of class A and class B. So how does this work ? How can the void return type accept returned objects of class A and class B ?

Thanks
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:My confusion is, methods callToAClassprintAndReturnSomething() and callToBClassprintAndReturnSomething() , both have void return type as void. But methods being called in them printAndReturnSomething() and printAndReturnSomething() both have return types of objects of class A and class B.


You can always call a method that returns something from a method that returns void. Is this what you are asking or have I not understood the question?

Another thing is I think you are assuming that methodNotInB() is not available to the class B. It is not the case.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:My confusion is, methods callToAClassprintAndReturnSomething() and callToBClassprintAndReturnSomething() , both have void return type as void. But methods being called in them printAndReturnSomething() and printAndReturnSomething() both have return types of objects of class A and class B. So how does this work ? How can the void return type accept returned objects of class A and class B ?



If method X calls method Y, the return type of X has nothing to do with the return type of Y. Your main method returns void, right? If we couldn't call non-void methods from void methods, then it would be impossible to ever call them. Main is void, so it could only call non-void methods, which in turn would only be able to call non-void methods, and so on.



In what way are you thinking that the x or the y above have anything to do with foo()'s return type? (I'm not being sarcastic here. I'm sincerely asking, in case you're still confused, so that I can try to understand the way you're picturing these pieces fitting together.)

Finally, it just occurred to me what your might be getting confused with. Possibly one of these:



In the first case, if append() returned void, we couldn't do that. It would be like trying to do void.append("a"). But since append() returns a reference to a StringBuilder, we can use the result of append as a StringBuilder reference just like any other SB reference expression.

In the second case, if currentTimeMillis() returned void, it would be like trying to call println(void). But it returns a long, so we're invoking the println(long) method.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replies.

I got it cleared
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic