• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Sylvan's Mock Question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: [Check all the correct answers]
Given the following calss definition:
1. public class DervedDemo extend Demo {
2. int M, N, L;
3. public DerivedDemo (int x, int y) {
4. M = x; N = y;
5. }
6. public DerivedDemo ( int x) {
7. super(x);
8. }
9. }
Which of the follwing constructor signatures MUST exist
in the Demo class for DerivedDemo to complie correctly?
a public Demo(int a, int b)
b public Demo(int a)
c public Demo()
The answer are b and c.
Could you explain why c is necessary ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one concept in constructors and that is
the objects should be constructed from top to bottom.
So when you create a derived demo from the constructor ( int x , int y) it automatically puts a implicit call to super() . But here you have one more constructor that calls like super(x).
now if the demo class has a constructor like demo( int x) then JAVA compiler does not provide the class with a default constructor . That is the explanation.
Beaware , there will be many questions on this concept in exam
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Daniel Liu:
Question: [Check all the correct answers]
Given the following calss definition:
1. public class DervedDemo extend Demo {
2. int M, N, L;
3. public DerivedDemo (int x, int y) {
4. M = x; N = y;
5. }
6. public DerivedDemo ( int x) {
7. super(x);
8. }
9. }
Which of the follwing constructor signatures MUST exist
in the Demo class for DerivedDemo to complie correctly?
a public Demo(int a, int b)
b public Demo(int a)
c public Demo()
The answer are b and c.
Could you explain why c is necessary ?


Because unless you make a specific call to a constrictor in a super class, the default constrictor is called. In the constrictor
public DerivedDemo (int x, int y) {
M = x; N = y;
}
no called is made to super so, Demo() is called.
Now since there is a call to super in the other constructor to
super(int) you have to have a constrictor Demo(int) defined.
When you define a constrictor, a default one isn't automatically generated and must be explicatly defined.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you pls. tell me the sullyvan Mock exam address.
 
Daniel Liu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.geocities.com/SiliconValley/Screen/5046/testpass.htm

Originally posted by Surya K:
Could you pls. tell me the sullyvan Mock exam address.


reply
    Bookmark Topic Watch Topic
  • New Topic