• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Question on Construtor

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : http://www.jchq.net
a) A Constructor may not be declared as private
b) A Constructor with no arguments will be the default constructor
c) Constructors cannot be overloaded
d) A call to constructor in a parent class can only be made from with in a
constructor

For the above question, i marked b) as the correct answer.
But they have given d) is the correct answer.

class Super
{
Super(){}
}
class Sub extends Super
{
Sub()
{
Super();
}
public void submethod()
{
Super super = new Super();// making call to super constructor
}
}

We are making call to Super() constructor in submethod() method.
Then option d) is wrong. Is this question cannot refer the above?

Rami
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Source : http://www.jchq.net
a) A Constructor may not be declared as private
b) A Constructor with no arguments will be the default constructor
c) Constructors cannot be overloaded
d) A call to constructor in a parent class can only be made from with in a
constructor

For the above question, i marked b) as the correct answer.
But they have given d) is the correct answer.

class Super
{
Super(){}
}
class Sub extends Super
{
Sub()
{
Super();
}
public void submethod()
{
Super super = new Super();// making call to super constructor
}
}

We are making call to Super() constructor in submethod() method.
Then option d) is wrong. Is this question cannot refer the above?

Rami




Hi Ram...
Here b cannnot be the answer because
The default constructor is no-arg constructor is true..
But the no arg constructor is not the default constructor..

You can only call the superclass constructor from the sub class constructor and it should be the first statement inside the subclass constructor...

Super super = new Super();// making call to super constructor

Here you are creating an object of class Super new Super() and not calling super();

Super super = new Super();// making call to super constructor

This line also won't compile since super is a keyword and cannot be used as an identifier.....
Hope this clears your doubt...

Correct me if I am Wrong......
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks kapil.
that cleared my doubt.
by hand i have written the code. So by mistake i typied super.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic