Quick inheritence question

A

andymconline

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
 
L

lord.zoltar

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.
 
J

Jason Cavett

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).
 
J

Jason Cavett

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.
 
A

andymconline

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
 
P

Patricia Shanahan

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
 
L

Lew

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/.
 
L

Lew

Jason said:
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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top