aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Superclass that requires any of its subclasses to create a no-arg constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Superclass that requires any of its subclasses to create a no-arg constructor?" Watch "Superclass that requires any of its subclasses to create a no-arg constructor?" New topic
Author

Superclass that requires any of its subclasses to create a no-arg constructor?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Write a superclass that requires any of its subclasses to create a no-arg constructor. Write a subclass for this superclass.

What is it supposed I have to do here?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You have to write really ugly and fragile code, that's probably based on reflection. If this is an exercise, that's fine, but you really shouldn't do it in a real project.

Take a look at the Class.newInstance() method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Joey Sanchez wrote:Write a superclass that requires any of its subclasses to create a no-arg constructor. Write a subclass for this superclass.

What is it supposed I have to do here?



And if this is a homework assignment for some beginners class, then I recommend asking the teacher for clarification, because I highly doubt that reflection is on any beginners java class curriculum.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

That was one of coding exercises in OCP Java SE 6 Programmer Practice Exams of Bates and Sierra. This exercise is part of chapter 4 Coding exercises (prototype/research/study projects) Section 5: OO Conceptss 5.f

Thanks Stephan van Hulst, I'll take a look at that method.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Are you sure you're quoting the exercise correctly? Because if I've interpreted that correctly, the only way to do it is using techniques that are not in the OCPJP syllabus.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Yes, that's exactly what the book says. page 150. I read it twice.
It has to be an easier explanation, but we don't know yet :s
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Joey Sanchez wrote:Yes, that's exactly what the book says. page 150. I read it twice.
It has to be an easier explanation, but we don't know yet :s



How about this? In all the years, and all the students who has gone through K&B, this is the first time, at least on the ranch, that this was every mentioned. This is the first time that anyone mentioned that K&B required that students know how a class can enforce it's subclass to have a constructor with a certain signature.

Don't know what to tell you -- but if there is a way to do it without reflection, I don't see it. Sorry.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


BTW... since there is zero chance that K&B is requiring reflection, here is how to do this:

The Super Class


The Sub Class


Running the test program -- the main method in the sub class -- will print the error message. Unless of course, you uncomment the no-arg constructor.

Henry
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
I wonder whether K&B might simply have meant, write a superclass that prevents its subclasses from using the default no-args constructor?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

I suspect you're right, or it's something similar. I've had a look at the section we're talking about now. It is as has been quoted in this thread, but looking at the other questions around it there's no way anything complicated was intended.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Thank Henry Wong, that could be a way!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Joey Sanchez wrote:Thank Henry Wong, that could be a way!


As many have already stated, I highly doubt it. It is highly unlikely that any beginners book would expect you to know (or even teach) the reflection libraries.


In rereading my previous post, I understand why you were confused..... I gave the solution because the solution *is* using the reflection libraries -- and since K&B can't be talking about the reflection libraries, I was not taking a learning exercise from you.

However, I guess it could have been read as an alternate solution to using the reflection libraries. Sorry, for the confusion.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Superclass that requires any of its subclasses to create a no-arg constructor?