Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Multiple super statements

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



output: from animal
program over.


can we have more than one super calls ? and why doesnt print that method's output or an compiler error.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two printyourself methods there.

- A static printyourself() method in Animal
- A non-static printyourself(int) method in Horse

In line 26 you're calling it without an argument. So you're calling the static method. Change it to:
and see what happens.
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes, you can have multiple 'super statements' in a method. super is a java keyword that refers to the base class. In the above mentioned method, you are executing the method printyourself() and eat() defined in the base class, i.e., Animal.

However, you cannot have multiple 'super statements' to call the super class constructor in a derived class. For example, the constructor of class Horse tries to call multiple constructors from the base class Animal and will fail to compile:


cheers
Mala
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mala, I am wondering if one could encounter a specific question like this in the exams; invoking super twice on a constructor?? However I didnt know about this before!...I cant trust them, because in the exams I told you I failed, I got a question that invoked run() method THREE times, I chose compiler error and guess what?? I got it wrong! ... The code compiles and runs perfectly.
 
Mala Gupta
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ikpefua,

Again, the key to understand the concepts and retain them longer is to understand why and how.

What is the job of a constructor? It defines code that executes when you create an object of a class. What happens when you have a base class - derived class arrangement? In this case, an object of a derived class contains within it an object of a base class. When you create an object of a derived class, an object of its base class also gets created. Examine the following code which prints Emp and Person:



The call to a base class contructor was implicit in this case. However, if you wish, you can make an explicit call to the constructor of a base class and call any other base constructor (a contructor that defines method parameters), if you have defined them.

Now, multiple calls to base class contructor essentially means that you are trying to initialize the base class object more than once, which isn't allowed!

cheers
Mala
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:I got a question that invoked run() method THREE times, I chose compiler error and guess what?? I got it wrong! ... The code compiles and runs perfectly.



To start a Thread, you are supposed to call its start method, which in turn calls its run method. If you call the run method yourself, you will not be able to execute your class as a 'Thread'. In this case, the method run will execute like any other user defined method.

cheers
Mala
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mala...Thanks for your reply, I knew that before, I was just trying to let you know how a fell on an exam 'Trip-Trap', thinking that a run method could not be invoked twice, even if invoking it directly meant a normal invocation, my question refers to the keyword 'super' I am wondering if one could encounter an exam question directly testing your knowledge on a multiple call to super??...
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:@Mala...Thanks for your reply, I knew that before, I was just trying to let you know how a fell on an exam 'Trip-Trap', thinking that a run method could not be invoked twice, even if invoking it directly meant a normal invocation, my question refers to the keyword 'super' I am wondering if one could encounter an exam question directly testing your knowledge on a multiple call to super??...


I don't see why not - it's possible. Also possible - and trickier (because it's less obvious) - would be a constructor where a call to super() wasn't the first line, which is also not allowed.
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:I am wondering if one could encounter an exam question directly testing your knowledge on a multiple call to super??...



Yes, its very much possible.

cheers
Mala
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Also possible - and trickier (because it's less obvious) - would be a constructor where a call to super() wasn't the first line, which is also not allowed.



I am afraid I didn't get your point here. A constructor doesn't allow a call to base class constructor, if it isn't the first line. Then how is this possible?

cheers
Mala
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mala Gupta wrote:I am afraid I didn't get your point here. A constructor doesn't allow a call to base class constructor, if it isn't the first line. Then how is this possible?


That's my point. I wouldn't put it past them to slip in a super() call somewhere other than the first line, to see if you notice.
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:That's my point. I wouldn't put it past them to slip in a super() call somewhere other than the first line, to see if you notice.



Matthew,

My mistake! I guess I did not read your initial comment carefully.

cheers
Mala
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mala...@Matthew... Thanks a lot!, I have just learnt about the last thing I did NOT know about constructors.
 
priyadharshini vijayaraghavan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mala and matthew... I understood it.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic