Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21196
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21196
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21196
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Henry Wong, that could be a way!
 
Henry Wong
author
Marshal
Pie
Posts: 21196
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic