• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt from SCJP mock test

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Prajakta Vaidya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic