aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt from SCJP mock test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt from SCJP mock test" Watch "Doubt from SCJP mock test" New topic
Author

Doubt from SCJP mock test

Prajakta Vaidya
Greenhorn

Joined: Oct 10, 2007
Posts: 10
Here is 1 Q in 1 of the SCJP mock tests on the net -

class Super{

String name;

Super(String s){

name =s ;

} }

class Sub extends Super{

String name;

Sub(String s){

name=s;

}

public static void main(String args[]){

Super sup = new Super("First");

Sub sub = new Sub("Second");

System.out.println(sub.name + " " + sup.name);

} }

I tried to run the code it gives compiler error for Super() constructor.

When I added no-args constructors for both the classes it worked fine, I don't get the reason behind the error.

Can anyone please help me ?

Thanks,

- Prajakta
Gaurav Bhatia
Ranch Hand

Joined: Jan 01, 2007
Posts: 49
Whenever a call to derived class constructor is made, it further calls the constructor of base class.
If there is no constructor in base class then it will insert a no argument constructor. But in case there exists a parameterized constructor in base then then it should be specifically called using .

In your case the base class contains a constructor and there is no call to this constructor from the constructor of derived class. So, it failed.

Hope this clarifies.


~Gaurav<br />SCJP5
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
Hi,
Is there is any argumented constructor in the super class , the subclass has to call the superclass constructor,voilation leads to compilation error.

In general what happens is ..

class SuperClass
{
SuperClass()
{
}
}
class SubClass extends SuperClass
{
SubClass()
{
}
}
After compilation the generated code will be like this..

class SuperClass
{
SuperClass()
{
super();
}
}
class SubClass extends SuperClass
{
SubClass()
{
super();
}
}

here when you are creating object for SubClass ,the constructor in the SubClass will call the super class construtor. If you understand this you can get the answer easily.
see the following code..
class SuperClass
{
SuperClass(int a)
{
}
}
class SubClass extends SuperClass
{
SubClass()
{
}
}
Here ,the generated code is...

class SuperClass
{
SuperClass(int a)
{
super();
}
}
class SubClass extends SuperClass
{
SubClass()
{
super(); // Here is the problem ,the control where to go ..no way
// to rectifiy the problem, We have to give manually
// like this for ex:- super(12);
}
}


Hence we are the responsibilty for to call the super class construtor ,using super(parameters);

I hope you understood .. give me your valuable FEEDBACK.


Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
Hi,
Is there is any argumented constructor in the super class , the subclass has to call the superclass constructor,voilation leads to compilation error.

In general what happens is ..

class SuperClass
{
SuperClass()
{
}
}
class SubClass extends SuperClass
{
SubClass()
{
}
}
After compilation the generated code will be like this..

class SuperClass
{
SuperClass()
{
super();
}
}
class SubClass extends SuperClass
{
SubClass()
{
super();
}
}

here when you are creating object for SubClass ,the constructor in the SubClass will call the super class construtor. If you understand this you can get the answer easily.
see the following code..
class SuperClass
{
SuperClass(int a)
{
}
}
class SubClass extends SuperClass
{
SubClass()
{
}
}
Here ,the generated code is...

class SuperClass
{
SuperClass(int a)
{
super();
}
}
class SubClass extends SuperClass
{
SubClass()
{
super(); // Here is the problem ,the control where to go ..no way
// to rectifiy the problem, We have to give manually
// like this for ex:- super(12);
}
}


Hence we are the responsibilty for to call the super class construtor ,using super(parameters);

I hope you understood .. give me your valuable FEEDBACK.
Mary John
Ranch Hand

Joined: Sep 17, 2007
Posts: 109
When your super class has a constructor defined with arguments then you should explicitly call that super constructor in the subclass constructor's first line and give the required arguments to the super constructor.

Since your superclass has a constructor with arguments defined, Compiler will not provide a default no-arg constructor in your superclass.It will only put to a default call to super() fromyour subclass constructor which currently you wont have.

Hope its clear


SCJP 5.0<br />SCJD working on it
Prajakta Vaidya
Greenhorn

Joined: Oct 10, 2007
Posts: 10
Thnx

Actually I thought only from the no-args constructor of the subclass the super() gets called, and in this case no object of Sub class is created like Sub sb = new Sub(), compiler won't complain
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
HiPrajakta,
For your code just provide consructor for Super class your code will compile and run becuse of the Constructor Rules you have to do that.
Just remember the rules for constructor it will be easier for you debug the code.
below is the rule for your code ---
Rules For Constructor [
If you want a no-arg constructor and you've typed any other constructor(s)
into your class code, the compiler won't provide the no-arg constructor (or
any other constructor) for you. In other words, if you've typed in a constructor with arguments, you won't have a no-arg constructor unless you type it in
yourself !

Hope your doubt got clear now...
[ October 10, 2007: Message edited by: Divya Gehlot ]

SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
Hi Prajakta Vaidya and All JAVA Lovers,
I request you that please reply with feedbacks. Because most of the members are answering the questions with lot of patience. If there are mistakes in the way of presentation they will rectify.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt from SCJP mock test