This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes no-arg constructor problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "no-arg constructor problem." Watch "no-arg constructor problem." New topic
Author

no-arg constructor problem.

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
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 :-)


Ping & DNS - my free Android networking tools app
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
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

Joined: Jan 28, 2008
Posts: 5575

Hint: How do you make sure one particular object has no-args constructor?
another Hint: SearchFirst ;)
Alan Cowap
Greenhorn

Joined: Oct 16, 2012
Posts: 6

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 is my .sig. There are many .sigs like it, but this one is mine.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 10, 2012
Posts: 70
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

Joined: Oct 16, 2012
Posts: 6

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

Joined: Jan 28, 2008
Posts: 5575

Have a look at Henry answer . Hope that help you .
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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

Joined: Sep 10, 2012
Posts: 70
Seetharaman Venkatasamy wrote:Have a look at Henry answer . Hope that help you .

thanks for the link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: no-arg constructor problem.
 
Similar Threads
constructor giving headache
Constructor Chaining
why there is no argument thread constructor?
inheritence related questions in Java
Constructors in Super Class