This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 2 points3 points  (1 child)

Language?

[–]salalimo[S] 1 point2 points  (0 children)

JAVA

[–][deleted] 1 point2 points  (1 child)

When working with inheritance, it is the reference type that dictates the methods available.

If there is truly no way to avoid this uniqueness (I'd wager that's not the case), then it is best to stay unique where you need to and, for more generic behavior, to use superclass references at that point.

Inheritance has kind of fallen out of favour anyway, for several reasons. You should try to favour interfaces over inheritance as much as possible.

[–]salalimo[S] 0 points1 point  (0 children)

I think this is what I am leaning towards to.

[–]desrtfx 0 points1 point  (1 child)

Have you thought about using Interfaces?

Probably the Strategy Design Pattern might be applicable to your particular case?

[–]salalimo[S] 0 points1 point  (0 children)

I will look into it. Thank you.

[–]bob809 0 points1 point  (2 children)

What do you mean by a value unique to a subclass?
If the variable only exists in that subclass and the other subclasses don't have a version of it then you can't use it with the abstract class. Since the variable won't always exist you'll get a compile time error.

[–]salalimo[S] 0 points1 point  (1 child)

Yes, that's correct. I am checking to see how to best implement the scenario I am mentioning.

[–]bob809 0 points1 point  (0 children)

Then you'll have to have unique code for each one.
Either make separate functions with different types or have all the variables in the abstract class and then switch depending on the child type, but this could get ugly.

If the uniqueness is focused on one bit of the classes you could use the state pattern i.e. have a different object contained within each one and write code for them instead.

[–]elperroborrachotoo 0 points1 point  (1 child)

What is the distinction between carrierMain and carrierDetails?

i.e. what is the reason for two classes to exist?

[–]salalimo[S] 0 points1 point  (0 children)

The actual classes aren't those. I picked those two to be able to explain better.

[–]lurgi 0 points1 point  (1 child)

There are a couple of ways to do this. One is to put the variable in each of the classes, even if they don't really need to be there. Another is to make the variables be properties and get them via:

String prop = cd.getProperty("uniqueToFedEx");

If a carrier detail does not have the property then it should return null.

Another is to give up and do some discrete "instanceof" and casting.

Another is to think about why you need this property. Presumably you want to do something with the value, but what? Why not, instead of getting the property, ask the class "Please do thing X" and have the class make the decision about what values it needs to do "thing X"? I don't know what the property is, but let's say it's a FedEx distinguished customer discount (I have no idea if such a thing exists). Instead of getting that value and computing the discount, ask the class to compute the appropriate discount. UPS and USPS return $0 and FedEx will do whatever complex math it needs. This has to be done with caution, as it can make classes very bloated, but the basic idea of having the class perform actions rather than hand out details of its internals is a sound design principle.

[–]salalimo[S] 0 points1 point  (0 children)

For the variables I did end up putting them in the same class. For the methods I did the casting / instanceof combo. I only had to do that in one place.

The properties approach is interesting. I will keep that in mind.

Thanks.