• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

no-arg constructor problem.

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, exactly, do you have so far, and where are stuck making progress? (Also, you may want to insert a "please" into the last sentence; it sounds so much nicer :-)
 
Himai Minh
Ranch Hand
Posts: 1295
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't really know what exactly the question is. But here is an example.

class Super{

//once you define an arg constructor, you won't have a non-arg constructor unless you define one.
Super(int i){
....
}

Super() {...}

}
class Sub extends Super{
Sub (){
super() ; // is there implicitly. If you don't have this line, it will be called.

}

}

What is Super class does not have Super() constructor?


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: How do you make sure one particular object has no-args constructor?
another Hint: SearchFirst ;)
 
Alan Cowap
Greenhorn
Posts: 6
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma ishu wrote:Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.


Hi Sharma,

A superclass can't require it's subclasses to create a no-arg constructor (i.e. a no-arg constructor in the subclass).
Furthermore, it's good OO that a superclass should have no 'knowledge' of it's subclasses.

Regards,
Al
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:What, exactly, do you have so far, and where are stuck making progress? (Also, you may want to insert a "please" into the last sentence; it sounds so much nicer :-)

Can you please give a super-class and it's sub-class code that satisfy the above criteria as soon as possible.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Cowap wrote:
sharma ishu wrote:Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.


Hi Sharma,

A superclass can't require it's subclasses to create a no-arg constructor (i.e. a no-arg constructor in the subclass).
Furthermore, it's good OO that a superclass should have no 'knowledge' of it's subclasses.

Regards,
Al

This question is from "practice exams book by kethy and bates" edition 2011 chapter-4 page-150. So, I think it should have a solution.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:Hint: How do you make sure one particular object has no-args constructor?
another Hint: SearchFirst ;)

Can you please give me a possible solution. It's urgent.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma ishu wrote:Can you please give me a possible solution. It's urgent.

Hi Sharma, Please EaseUp .

Coming to your question - did you follow on my second hint ?
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
sharma ishu wrote:Can you please give me a possible solution. It's urgent.

Hi Sharma, Please EaseUp .

Coming to your question - did you follow on my second hint ?

I did follow it. but found nothing. Now kindly answer my question.
 
Alan Cowap
Greenhorn
Posts: 6
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma ishu wrote:
Alan Cowap wrote:
sharma ishu wrote:Write a super class that requires each of its sub-class to create a no-arg constructor. Also write its one sub-class.
Help me with this problem.


Hi Sharma,

A superclass can't require it's subclasses to create a no-arg constructor (i.e. a no-arg constructor in the subclass).
Furthermore, it's good OO that a superclass should have no 'knowledge' of it's subclasses.

Regards,
Al

This question is from "practice exams book by kethy and bates" edition 2011 chapter-4 page-150. So, I think it should have a solution.


With respect to the K&B, the question could be written less ambiguously.
"Write a superclass that requires any of its subclasses to create a no-arg constructor [in the superclass]. Write a subclass for this superclass.
With the question in this context, the answer given above by Himai Minh is what you want.

Aside:
One could equally interpret the question as:
"Write a superclass that requires any of its subclasses to create a no-arg constructor [in the subclass(es)]. Write a subclass for this superclass.
In which case my previous answer would suffice.

Regards,
Alan
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Henry answer . Hope that help you .
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Cowap wrote:With respect to the K&B, the question could be written less ambiguously.
"Write a superclass that requires any of its subclasses to create a no-arg constructor [in the superclass].


I'm not convinced that's less misleading - how can a subclass create a constructor in the superclass? But you're write it should be phrased differently. The thread Seetharaman linked to discussed likely interpretations (and also had a solution to the problem as stated - though it relied on reflection and definitely wasn't the intended solution of the exercise), and I suspect what you intended the above to mean is right.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:Have a look at Henry answer . Hope that help you .

thanks for the link.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic