File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Abstract class with abstract class passed to constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract class with abstract class passed to constructor" Watch "Abstract class with abstract class passed to constructor" New topic
Author

Abstract class with abstract class passed to constructor

Edmund Castermund
Ranch Hand

Joined: May 09, 2007
Posts: 81
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

Joined: Oct 21, 2000
Posts: 4339
    
    2

i might be wrong but...
there is nothing wrong with the cast.

SCJP
Visit my download page
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: May 09, 2007
Posts: 81
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

Joined: May 09, 2007
Posts: 81
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class with abstract class passed to constructor
 
Similar Threads
method overriding and visibility promotion.
Instance of an abstract class
overriding JDBCRealm
Overriding Constructor
Calling EJB from servlet