Tuesday, June 19, 2007

that a Java interface can be kinda handy

I mostly code in c++ but I learned Java in college and I'm taking the time to brush back up on it. I quickly ran into interfaces and abstract classes. C++ has prototypes and inheritance, but none of it really seems to match up that well with interfaces or abstract classes in Java.

An interface is a contract that states certain functions will always be available for an object that implements this. This allows polymorphism in Java with out using strict inheritance.

For example: a BasketBall and a SoccerBall object could both inherit from a super class called Ball that had a virtual function that allowed them bounce. This would follow from the C++ mindset as well as abstract classes from Java.

Now take BasketBall and SoccerBall and have them both implement a Bouncing interface. There is no real equivalent for this in C++. In Java both the BasketBall object and the SoccerBall object will have the ability to bounce regardless of any other differences in the classes. There are no parent classes for either BasketBall or SoccerBall; there is only the guarantee that certain functions exist.

Lesson Learned : Java is still more than a little odd, but it seems to at least have its reasons.

No comments: