• 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

Mock Question ...

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following piece of code why is not Base getting printed ?

Thanks in advance.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a trick question. The Base.Base method is not a constructor. A constructor does not declare a return type.
 
Salim Mohamed
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed it completely.Thanks Dan.Isnt there a rule that method cannot have the same name as the constructor ? Will the compiler be able to differentiate the mistake the programmer makes by adding a return type to the constructor ?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salim -
This is a trick you are almost sure to see on the exam. A class, and it's constructors, and a method of the class can all have the same name. :roll:
Also watch out for questions that appear to be about overloaded constructors that are actually methods in disguise!
-Bert
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy -- just wanted to add one more thing:
You might see something like:
public class Foo {
void Foo() { }
void Foo(String s) { }
}
...and then be asked a question relating to the default constructor. In the code above, if you do not recognize that these are METHODS and not constructors, then you might think, "I won't get a default constructor in class Foo because I've already provided a constructor (or two). But of course, because these are not constructors, there IS no constructor in class Foo and so the compiler will put the default constructor in the Foo class
cheers,
Kathy
[Now totally and completely jealous after seeing a picture of Jessica with her MINI. ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kathy Sierra:
[Now totally and completely jealous after seeing a picture of Jessica with her MINI. ]



But of course, the real reason why I recommend Kathy's book so highly is because of this example:
reply
    Bookmark Topic Watch Topic
  • New Topic