• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Default constructor

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think the answer should be d, but correct ans is c??
I was under impression that if you have argumented construtor you "must" also have default construtor defined, since it is provided implicitly.
Any missing part for me?
Advannce Thanx
-PC
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...I missed "not" part in sentence
--since it is not provided implicitly if you have any other constructor.
-PC
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Priyanka,
The answer is c. Your assumption (answer d) would have been correct if the Bclass constructor did not call super(3).
Say, the Bclass constructor has simply been omitted or has nothing in its body, then a no-argument super class constructor would have been called by default as you said.

The choice would be d for this case :
class AClass {
static int x;
AClass(int x) { this.x = x; }
}
public class BClass extends AClass {
BClass() {} // or if this line has been commented out.
public static void main(String[] args) {
BClass BC = new BClass(); System.out.println(x);
}
}
Hope this helps.
 
Kalai Ganesh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even for this case you'll get a compile time error because there is no constructor in Aclass that accepts no arguments.
BClass() { super(); }
In your program, you are safe since a matching super class constructor is called in Bclass constructor.
[ October 15, 2003: Message edited by: Kalai Ganesh ]
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is (c) because there is no default
constructor created for A and B calls the constructor
with argument which is perfectly fine!
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks buddies!
That helped
-PC
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a question that is a subclass can extends a static variable
from superclass?
I know a static variable is shared by all object, but it can share by an instance of subclass?

I am really appreciate it.
 
I like tacos! And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic