Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class with abstract class passed to constructor

 
Edmund Castermund
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I can't believe I don't know the answer to this....but if I have a set of abstract classes like so:








 
Randall Twede
Ranch Hand
Posts: 4390
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i might be wrong but...
there is nothing wrong with the cast.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason why you can't just change the parameter in the DogGroomer constructor to be a Dog object?
 
Edmund Castermund
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k,
that's how I've been doing it, but I wasn't sure if it was considered bad design nor not. Under this scenario, the DogGroomer object will have both a Dog variable and a Pet variable and I wasn't sure if that was a problem or not. This is one of the (several) areas where I have ambiguity about little java 101 questions that bug me.
 
Edmund Castermund
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Is there any reason why you can't just change the parameter in the DogGroomer constructor to be a Dog object?


that gives me a compile error... you have to have the constructor based on the superclass, and I was unclear as to how exactly that works and what the requirements are.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just thinking of this, which doesn't give a compiler error. The constructor doesn't have to relate to the superclass (though the super() call within it does).

However, I think your instincts are right - I definitely wouldn't have two variables for the same thing.

It's difficult to be sure, because I assume there's more to add to the class. But have you considered making the class generic?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic