For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2004 > Feeling dumb









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Feeling dumb
Erik Midtskogen

2004-07-23, 3:58 pm

I have a problem that I feel must have an easy solution. I want to be
able to instantiate a class using an instance of a superclass of that
class that has already been instantiated. Calling the superclass
constructor from the constructor of my subclass is a less than optimal
solution, because creating the superclass object requires expensive
database calls that I'd prefer to avoid making redundantly.

Conceptually, what I'd like to be able to do is something like this:

class Subclass extends Superclass {
public Subclass(Superclass sc) {
this.super = sc;
}
....subclass-specific extensions and overrides....
}

Of course, this doesn't work, for obvious reasons.

I could copy data members from the superclass to the subclass by hand
in the constructor of the subclass, but this strikes me as clunky and
wasteful.

Any other ideas?
Bryce

2004-07-23, 3:58 pm

On 23 Jul 2004 07:31:11 -0700, erik@firmsupport.com (Erik Midtskogen)
wrote:

>I have a problem that I feel must have an easy solution. I want to be
>able to instantiate a class using an instance of a superclass of that
>class that has already been instantiated. Calling the superclass
>constructor from the constructor of my subclass is a less than optimal
>solution, because creating the superclass object requires expensive
>database calls that I'd prefer to avoid making redundantly.
>
>Conceptually, what I'd like to be able to do is something like this:
>
>class Subclass extends Superclass {
> public Subclass(Superclass sc) {
> this.super = sc;
> }
> ....subclass-specific extensions and overrides....
>}


Try this:

class Superclass {
public Superclass(Superclass sc) {
this.somefield = sc.somefield
// or, if its not public, but you have getters and setters:
this.somefield2 = sc.getSomefield2();
}
}

class Subclass extends Superclass {
public Subclass(Superclass sc) {
super(sc);
}
}

>Of course, this doesn't work, for obvious reasons.
>
>I could copy data members from the superclass to the subclass by hand
>in the constructor of the subclass, but this strikes me as clunky and
>wasteful.


Oh, that's what I do up there. That's the only way I know of, unless
you use reflection

>Any other ideas?



--
now with more cowbell
Eric Sosman

2004-07-23, 3:58 pm

Erik Midtskogen wrote:
> I have a problem that I feel must have an easy solution. I want to be
> able to instantiate a class using an instance of a superclass of that
> class that has already been instantiated. Calling the superclass
> constructor from the constructor of my subclass is a less than optimal
> solution, because creating the superclass object requires expensive
> database calls that I'd prefer to avoid making redundantly.


A superclass constructor must be called, and if you don't
do it explicitly the compiler will insert a call for you. But
it isn't necessary to call "the" superclass constructor; any
constructor will suffice -- and you can choose which superclass
constructor you want to call. In particular, you could call one
that doesn't consult the database, and doesn't store any database-
derived values in its fields.

> [...]
> I could copy data members from the superclass to the subclass by hand
> in the constructor of the subclass, but this strikes me as clunky and
> wasteful.


There is no way to transform an existing Superclass object
into a new Subclass object. Every time you create a new
Subclass object, you automatically create a new Superclass
object at the same time. If you want to initialize the data
members of the new Superclass from an existing Superclass,
copying seems the way to do it.

> Any other ideas?


Have you considered using composition instead of inheritance?
Instead of extending Superclass, could you just make Subclass
hold a reference to its associated Superclass object?

class Subclass /* not a good name, now */ {
Superclass super;
public Subclass(Superclass super) {
this.super = super;
}
}

--
Eric.Sosman@sun.com

Erik Midtskogen

2004-07-28, 9:08 pm

Thanks, everyone! Somehow I thought there must have been some elegant
way of handling this type of situation through some special mechanism
built into the language. At least now I'm not feeling do dumb
anymore.

I did consider using reflection, but became clear that, for a small
project such as mine this would be not much easier than hand-copying
data members, and the resulting code would probably run more slowly.

Actually, the way I "solved" the problem was just to put the subclass
functionality directly into the superclass and do away with the
subclass altogether. It seems a shame to fatten up the superclass
this way, but this app is not even close to being memory-bound, so
there's no harm done.

At this point I just need to stop striving for computational poetry
and get this thing done.

Thanks!
--Erik

Eric Sosman <Eric.Sosman@sun.com> wrote in message news:<41012A22.4000904@sun.com>...
> Erik Midtskogen wrote:
>
> A superclass constructor must be called, and if you don't
> do it explicitly the compiler will insert a call for you. But
> it isn't necessary to call "the" superclass constructor; any
> constructor will suffice -- and you can choose which superclass
> constructor you want to call. In particular, you could call one
> that doesn't consult the database, and doesn't store any database-
> derived values in its fields.
>
>
> There is no way to transform an existing Superclass object
> into a new Subclass object. Every time you create a new
> Subclass object, you automatically create a new Superclass
> object at the same time. If you want to initialize the data
> members of the new Superclass from an existing Superclass,
> copying seems the way to do it.
>
>
> Have you considered using composition instead of inheritance?
> Instead of extending Superclass, could you just make Subclass
> hold a reference to its associated Superclass object?
>
> class Subclass /* not a good name, now */ {
> Superclass super;
> public Subclass(Superclass super) {
> this.super = super;
> }
> }

Hal Rosser

2004-07-28, 9:08 pm

When you said
"Calling the superclass constructor from the constructor of my subclass is a
less than optimal solution, because creating the superclass object requires
expensive database calls that I'd prefer to avoid making redundantly."
If the database calls are not needed in subclasses - maybe you should make a
separate class for those database calls.
You forget that a subclass "is a" superclass.
For instance if you create a class "WorkerBee" from its superclass of
'Employee" - the workerbee "is a" Employee.
So when you instantiate your subclass - remember it has all the methods and
variables of the superclass.
If "WorkerBee" does not need all the methods of "Employee" - then you can
over-ride the methods -
OR - maybe the WorkerBee class shouldn't extend Employee at all.
Plan your classes carefully and you can save yourself some code.



"Erik Midtskogen" <erik@firmsupport.com> wrote in message
news:33fcde33.0407230631.9c4c27a@posting.google.com...
> I have a problem that I feel must have an easy solution. I want to be
> able to instantiate a class using an instance of a superclass of that
> class that has already been instantiated. Calling the superclass
> constructor from the constructor of my subclass is a less than optimal
> solution, because creating the superclass object requires expensive
> database calls that I'd prefer to avoid making redundantly.
>
> Conceptually, what I'd like to be able to do is something like this:
>
> class Subclass extends Superclass {
> public Subclass(Superclass sc) {
> this.super = sc;
> }
> ....subclass-specific extensions and overrides....
> }
>
> Of course, this doesn't work, for obvious reasons.
>
> I could copy data members from the superclass to the subclass by hand
> in the constructor of the subclass, but this strikes me as clunky and
> wasteful.
>
> Any other ideas?



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 7/16/2004


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com