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

constructor signatures superclass

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

Which of the following constructor signatrues must exist in the Demo class for DerivedDemo to compile correctly?
A) public Demo( int a, int b )
B) public Demo( int c )
C) public Demo()
Answer: B and C
Why 'C' please explain....
[This message has been edited by Willie Toma (edited December 06, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Willie
It is b and c because in the first DerivedDemo constructor there is no explicit call to super, so the compiler will insert one to the default constructor - super().
In the second DerivedDemo constructor there is an explicit call to super(x) (where x is an int), so there must be a super class constructor that takes an int as its argument - Demo(int x).
a is not correct because there is no call to a superclass constructor that takes two ints as arguments.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
No,the answers given are correct.
All constructors will call the no arg constructor of its super class if no constrructor is explicitly provided.
public DerivedDemo(int x,int y)
has no constructor explicitly provided so it wil call no args constructor from its super class .If it is not present then the code will not compile.
public DerivedDema(int x)
has been explicitly provided with a constructor so here no args cons. of super class will not be called.
So for your DerivedDemo class to get compiled B and C should be right.Both B and C should be present in the superclass.
Hope it helps
Neha
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is no explicit call made in the code to the super the implicit call is always made to the super(); contructor. As in the constructor public DerivedDemo( int x, int y), the first line compiler check if it is a call for the super() or any this() for the constructor for the super class and the same class. Which is not there so compiler puts a call to the super of no parameter constructor.
Further,public DerivedDemo( int x), in this contructor in the body the first line calls the super classes same parameter constructor so that is why the answer is B and C rather than A and B.
I hope it helps.
Jennifer.
reply
    Bookmark Topic Watch Topic
  • New Topic