Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Construtor

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • 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
 
kapil kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks kapil.
that cleared my doubt.
by hand i have written the code. So by mistake i typied super.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic