Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Semi-Complex Inheritance Question- URGENT 3

Status
Not open for further replies.

BoulderBum

Programmer
Jul 11, 2002
2,179
US
Let's say I have an abstract base class:
BaseClass
Some subclasses down the inheritance tree pick up a common interface:
InterfaceClass


In my code, I'm hoping to use polymorphism to call common BaseClass functions, but at one point, I will need the capability provided by InterfaceClass.

My question is, should I have any problem casting from a BaseClass reference to an InterfaceClass when necessary?

I'm actively scrambling to redesign a project to do something like what's mentioned, but I want to avoid a roadblock if I become aware of potential problems.
 
Downcasting (casting to the base class or implemented interface from the derived class) should never be a problem

Upcasting should almost never be done. If you find a situation that requires upcasting it generally points out a bad design.

-pete
 
Just an idea (it might be a bad one) :

Can you make "BaseClass" implement a new "BaseInterface" and create another interface "SubClassInterface extends BaseInterface, InterfaceClass" and let your subclasses implement this interface. Then you don't need to cast and you can't get class cast exceptions.
 
I assume your Interface class is an interface. Then I do not foresee any problems. Since java provides single inheritance and not multiple inheritance, like C++(to be honest, I never used multiple inheritance in C++!), it's better to design your applications to pluggability - by using interfaces.

I had the same dilemma recently, when writing my middle tier for my jsp application using struts. One disadvantage about using all these components are, you are stuck with the way they do this. in this case, i try to implement an action strut that has to extend Action class. But then, I want to have an abstract class that has all the methods add, delete, update so all the sub classes are 'forced' to use to implement these methods.Guess what? it already inherited the struts' action class leaving me no choice but to implement an interface instead. The good thing is: i already design my components around interfaces instead of inheritance thus giving me the flexibility to move forward without worrying down the line that makes me say, "ooppss..."

hope this helps...

~za~
You can't bring back a dead thread!
 
holo,

Awesome idea! I hate you because I'll be rewriting some code in the near future.

I'm pretty new to the interface stuff. I actually don't care for it too much because I wish some of the implementation of the interface could be shared.
 
boulderbum,
pick up a design book, 'Java Design: Building Better Apps and Applets' by peter coad. This is an awesome design book that talks about programming with interfaces and not through inheritance. I betcha can get a used one for a cheap price at amazon.

> I wish some of the implementation of the interface could >be shared.
Unlike abstract methods, i think interface implements its own strictness when it comes to implementation; that is is forces the developer to adhere to the coding standard by forcing the developers to provide the implentations. Hence other developers that implement the same interface know exactly what they're looking for when they read each other's codes.

Java is a language that is easy to abuse, if we don't stick with the right paradigm and methodologies. Hence, only strict design schema can ensure that our implementations are savvy, in my humble opinion.


~za~
You can't bring back a dead thread!
 
At runtime if you need to check to see if the instance of BaseClass you're working with implements the interface, why not do something like

Code:
public void func(BaseClass obj){
if(obj instanceof InterfaceClass){
// now safe to cast as an interface
InterfaceClass ic = (InterfaceClass)obj;
}
}

Alternatively, you could just cast the BaseClass object as an InterfaceClass regardless, but then you run the risk and overhead of ClassCastException's.
 
"pick up a design book, 'Java Design: Building Better Apps and Applets' by peter coad. This is an awesome design book that talks about programming with interfaces and not through inheritance."

Sounds cool. I'll have to check the book out.

I actually prefer inheritance because of its flexibility in reusable code. Interfaces enable polymorphism, but some of the thrill of design is thinking of how to program in the generic and how to make objects scalable. Can you imagine having to code a JPanel if all of the methods were implemented as abstract interfaces? Blech!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top