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

mock test question need help

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,
this block of code compiles succesfully:
i thought that the constructor in Class B has a return
type, so it should not compile.
please tell where i am going wrong??
class A {
public A() {}
public A(int i) { this ();}
}
class B extends A{
public boolean B(String msg) {return false;}
}
class C extends B{
private C( ) { super( );}
public C(string msg) { this( ) ;}
public C(int i) { }
}
thanx,
prasanthi
}
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasanthi,
I also got fooled once like this. Please see the code once again. In class B the code you doubted is NOT a constructor. It is a regular METHOD which returns a boolean.
regds
maha anna
[This message has been edited by maha anna (edited November 07, 2000).]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha, ??? What makes the method not a constructor? If lesser mortals had said, I'd have doubted their word!!
Please explain why this is not a constructor.
Shashi
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha is right.
Constructors may have access specifiers but CONSTRUCTORS MUST NOT HAVE ANY RETURN TYPE. In class B, it is not a constructor but a general method with the name of the class itself.
 
prasanthi kothapa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi maha anna,

please explain why it's not a constructor.
i am very confused about it..
i will very grateful if you can help me on this..
thanks..
prasanthi
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasanthi,
When I try to instantiate an object of type B with a string as argument, it says, "Wrong number of arguments in the constructor of B", which means to say that it is not considering this as a constructor. Mebbe because the signature indicates a return value? Someone can help???
Thanks,
Aparna
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasanthi,
Maha is right. The JLS states a constructor as "the constructor declaration looks just like a method declaration that has no result type."
So the presence of a return type indicates that it CAN'T be a constructor.
hope that helps
Oliver
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
just remember this.
any function with the same name of the class and has a return type specified in the declaration is treated as a method. this also includes void.

sagar
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasanthini,
Sorry for the late reply. The difference bet a constructor and a regular method is the 'RETURN TYPE' at the definition. A constructor CAN NOT have a return type. A regular method can have the SAME NAME as the class, but SHOULD have a return type.
So , bottom line a constructor can't have any RETURN TYPE.
regds
maha anna
 
Something about .... going for a swim. With this tiny ad ...
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic