Forums Register Login

no-arg constructor problem.

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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 :-)
+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
Hint: How do you make sure one particular object has no-args constructor?
another Hint: SearchFirst ;)
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Have a look at Henry answer . Hope that help you .
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Seetharaman Venkatasamy wrote:Have a look at Henry answer . Hope that help you .


thanks for the link.
Montana has cold dark nights. Perfect for the heat from incandescent light. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1183 times.
Similar Threads
constructor giving headache
why there is no argument thread constructor?
Constructor Chaining
inheritence related questions in Java
Constructors in Super Class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:57:58.