Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Quick inheritence question

Reply
Thread Tools

Quick inheritence question

 
 
andymconline@googlemail.com
Guest
Posts: n/a
 
      01-04-2008
Hello all,

Would the following be considered bad practice...

I have a very simple bean called "SimpleTypeBean" which is constructed
as follows:

public class SimpleTypeBean {

private long id;
private String description;

public SimpleTypeBean() {
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

I then need to declare a very similar bean (in fact, it is identical
in terms of data types), except one of the identifiers is called lob
not id, so I have done the following:

public class LobBean extends SimpleTypeBean {

public LobBean() {
}

public long getLob() {
return super.getId();
}

public void setLob(long lob) {
super.setId(lob);
}

}

Is this considered bad practice or is this what I should be doing?

Many Thanks

Andy
 
Reply With Quote
 
 
 
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      01-04-2008
On Jan 4, 10:46*am, andymconl...@googlemail.com wrote:
> Hello all,
>
> Would the following be considered bad practice...
>
> I have a very simple bean called "SimpleTypeBean" which is constructed
> as follows:
>
> public class SimpleTypeBean {
>
> * private long id;
> * private String description;
>
> * public SimpleTypeBean() {
> * }
>
> * public long getId() {
> * * return id;
> * }
>
> * public void setId(long id) {
> * * this.id = id;
> * }
>
> * public String getDescription() {
> * * return description;
> * }
>
> * public void setDescription(String description) {
> * * this.description = description;
> * }
>
> }
>
> I then need to declare a very similar bean (in fact, it is identical
> in terms of data types), except one of the identifiers is called lob
> not id, so I have done the following:
>
> public class LobBean extends SimpleTypeBean {
>
> * public LobBean() {
> * }
>
> * public long getLob() {
> * * return super.getId();
> * }
>
> * public void setLob(long lob) {
> * * super.setId(lob);
> * }
>
> }
>
> Is this considered bad practice or is this what I should be doing?
>
> Many Thanks
>
> Andy


The subclass will have an id and lob.
What you could do: Create an abstract class "AbstractSimpleTypeBean"
that implements everything EXCEPT the id and lob. Then have subclasses
that implement id and lob as appropriate.
 
Reply With Quote
 
 
 
 
Jason Cavett
Guest
Posts: n/a
 
      01-04-2008
On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
> Hello all,
>
> Would the following be considered bad practice...
>
> I have a very simple bean called "SimpleTypeBean" which is constructed
> as follows:
>
> public class SimpleTypeBean {
>
> private long id;
> private String description;
>
> public SimpleTypeBean() {
> }
>
> public long getId() {
> return id;
> }
>
> public void setId(long id) {
> this.id = id;
> }
>
> public String getDescription() {
> return description;
> }
>
> public void setDescription(String description) {
> this.description = description;
> }
>
> }
>
> I then need to declare a very similar bean (in fact, it is identical
> in terms of data types), except one of the identifiers is called lob
> not id, so I have done the following:
>
> public class LobBean extends SimpleTypeBean {
>
> public LobBean() {
> }
>
> public long getLob() {
> return super.getId();
> }
>
> public void setLob(long lob) {
> super.setId(lob);
> }
>
> }
>
> Is this considered bad practice or is this what I should be doing?
>
> Many Thanks
>
> Andy


Two things going on here. First, the simple thing...

I think the important part here is, "One of the components is called
lob ***NOT*** id."

So, you're saying, LobBean extends SimpleTypeBean. So, because of
that, your LobBean has TWO long values (lob and id). I don't think
that's what you're really looking for. So, the simple solution is
that LobBean does not extend SimpleTypeBean.

Slightly more complicated, but makes more sense...

BUT, as you have noticed, both beans do share a description. And, if
you also noticed, "lob" and "id" are *exactly* the same thing...they
are both long values. So, the only difference is what you name them.
In my opinion, that's not a difference. Really, you could use Simple
as an LOB and be perfectly fine. (Of course, if LobBean has OTHER
differences, you would want to extend SimpleBean and then add the
additional features.) The point I'm trying to make is that id = lob
for your purposes. Don't make it more difficult than it is through
your variable names (which nobody is going to see anyway).
 
Reply With Quote
 
Jason Cavett
Guest
Posts: n/a
 
      01-04-2008
On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
> Hello all,
>
> Would the following be considered bad practice...
>
> I have a very simple bean called "SimpleTypeBean" which is constructed
> as follows:
>
> public class SimpleTypeBean {
>
> private long id;
> private String description;
>
> public SimpleTypeBean() {
> }
>
> public long getId() {
> return id;
> }
>
> public void setId(long id) {
> this.id = id;
> }
>
> public String getDescription() {
> return description;
> }
>
> public void setDescription(String description) {
> this.description = description;
> }
>
> }
>
> I then need to declare a very similar bean (in fact, it is identical
> in terms of data types), except one of the identifiers is called lob
> not id, so I have done the following:
>
> public class LobBean extends SimpleTypeBean {
>
> public LobBean() {
> }
>
> public long getLob() {
> return super.getId();
> }
>
> public void setLob(long lob) {
> super.setId(lob);
> }
>
> }
>
> Is this considered bad practice or is this what I should be doing?
>
> Many Thanks
>
> Andy


Something I also missed...

Having "setLob" and "getLob" does NOT hide "setId" and "getId." So,
if someone is using your beans, it could be very confusing to the
differences of what an lob is and what an id is.
 
Reply With Quote
 
andymconline@googlemail.com
Guest
Posts: n/a
 
      01-04-2008
On 4 Jan, 16:28, Jason Cavett <jason.cav...@gmail.com> wrote:
> On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
>
>
>
> > Hello all,

>
> > Would the following be considered bad practice...

>
> > I have a very simple bean called "SimpleTypeBean" which is constructed
> > as follows:

>
> > public class SimpleTypeBean {

>
> > private long id;
> > private String description;

>
> > public SimpleTypeBean() {
> > }

>
> > public long getId() {
> > return id;
> > }

>
> > public void setId(long id) {
> > this.id = id;
> > }

>
> > public String getDescription() {
> > return description;
> > }

>
> > public void setDescription(String description) {
> > this.description = description;
> > }

>
> > }

>
> > I then need to declare a very similar bean (in fact, it is identical
> > in terms of data types), except one of the identifiers is called lob
> > not id, so I have done the following:

>
> > public class LobBean extends SimpleTypeBean {

>
> > public LobBean() {
> > }

>
> > public long getLob() {
> > return super.getId();
> > }

>
> > public void setLob(long lob) {
> > super.setId(lob);
> > }

>
> > }

>
> > Is this considered bad practice or is this what I should be doing?

>
> > Many Thanks

>
> > Andy

>
> Something I also missed...
>
> Having "setLob" and "getLob" does NOT hide "setId" and "getId." So,
> if someone is using your beans, it could be very confusing to the
> differences of what an lob is and what an id is.



Thanks Jason. I think I will just remove LobBean and go with
SimpleTypeBean

Kind Regards

Andy
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-04-2008
wrote:
> On 4 Jan, 16:28, Jason Cavett <jason.cav...@gmail.com> wrote:

....
>> Having "setLob" and "getLob" does NOT hide "setId" and "getId." So,
>> if someone is using your beans, it could be very confusing to the
>> differences of what an lob is and what an id is.

>
>
> Thanks Jason. I think I will just remove LobBean and go with
> SimpleTypeBean


That is a good solution if, and only if, each of the bean's methods can
be described by a simple, clear comment. That will be the case if a
"lob" and an "id" do essentially the same job, in the context of the
interface to this bean, as well as having the same type.

Patricia
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-05-2008
andymconl...@googlemail.com wrote:
>> public class LobBean extends SimpleTypeBean {
>>
>> public LobBean() {
>> }
>>
>> public long getLob() {
>> return super.getId();
>> }
>>
>> public void setLob(long lob) {
>> super.setId(lob);
>> }
>>
>> }
>>
>> Is this considered bad practice or is this what I should be doing?


wrote:
> The subclass will have an id and lob.


True - the public parent methods publish a view of the 'id' as an alias for 'lob'.

> What you could do: Create an abstract class "AbstractSimpleTypeBean"
> that implements everything EXCEPT the id and lob. Then have subclasses
> that implement id and lob as appropriate.


Or just use a 'SimpleBean' where 'id' just happens to function as a 'lob',
talks to 'lob' columns of the data store, fills in 'lob' UI fields, and just
isn't named 'lob'.

Or just write another bean that is structurally identical with different field
names.

Inheritance isn't really about refactoring for the sake of refactoring. It's
to capture a modeled "is-a" relationship. If your 'LobBean' /is-a/
'SimpleBean' then inheritance is exactly right.

In this case it partly works - LobBean puts a facade on its parent that lets
it show an 'id' as if it were a 'lob'. You don't actually need the 'super'
decorations to resolve the calls since they're public. You still have that
'id' attribute though, and that doesn't match your logical model. So that
breaks /is-a/.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-05-2008
Jason Cavett wrote:
> Two things going on here. First, the simple thing...
>
> I think the important part here is, "One of the components is called
> lob ***NOT*** id."
>
> So, you're saying, LobBean extends SimpleTypeBean. So, because of
> that, your LobBean has TWO long values (lob and id). I don't think
> that's what you're really looking for. So, the simple solution is
> that LobBean does not extend SimpleTypeBean.
>
> Slightly more complicated, but makes more sense...
>
> BUT, as you have noticed, both beans do share a description. And, if
> you also noticed, "lob" and "id" are *exactly* the same thing...they
> are both long values. So, the only difference is what you name them.
> In my opinion, that's not a difference. Really, you could use Simple
> as an LOB and be perfectly fine. (Of course, if LobBean has OTHER
> differences, you would want to extend SimpleBean and then add the
> additional features.) The point I'm trying to make is that id = lob
> for your purposes. Don't make it more difficult than it is through
> your variable names (which nobody is going to see anyway).


Cogent and wise.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-05-2008
On Fri, 4 Jan 2008 07:46:01 -0800 (PST),
wrote, quoted or indirectly quoted someone who said :

>Is this considered bad practice or is this what I should be doing?


this is screwy since you still have getID defined.

The proper way to do this is define an abstract class without id or
lob, then subclass it once with id and once with lob.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick question, hopefully quick answer. ~misfit~ NZ Computing 0 12-28-2004 11:55 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Inheritence question Doug Nichols Perl 0 02-19-2004 01:08 PM
Question about logging and inheritence krzyber Java 1 11-20-2003 08:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57