Wednesday, February 23, 2011

POJO extends vs implements

In Java we have two way that we implement inheritance either one extends a super class or implements an interface.

In JavaEE frameworks you will see a lot of this and understanding the difference between the two is pretty important.

Interfaces in Java have no actual code to back up the Interface. In other words implementing an interface requires you to add in all the code for the specified members in the interface. So what's the big deal then with interfaces?! They don't save you time do they? Usually an interface provides a hook into a library or a function of the EE server, so usually an interface will hook one object into another or the server proper.

Extending an object, however, provides the base class' implementation if you're feeling a bit lazy. Usually you will extend base classes to make something more specific to your use case. Going from something like a data table to an Employee data table. In Java you only get to extend one super class, as opposed to interfaces (which you can implement as many as those you want). I remember coming from all my C++ to Java and being frustrated at this.


It is also important to know about generics, generics allow your class to apply to more than one data type. For example instead of extending data table into employee data table, one could make data table generic data table<?>. Then you could say data table<Employee> or data table<Student>. Generics are extremely helpful for making common web concepts, like pagination, thumb viewers, and so on. The reason being is that the functionality doesn't change based on data type (A picture swoosher does the same thing to PNG as it would to JPG).

Finally annotations play a huge role in EE development in Java. Annotations allow a developer to add meta-data to their objects. A server can then read in this meta-data and change how it will behave with the object. Annotations only change the user's behavior not the objects. Remember that!


So why the lesson in general Object Oriented Programming? I think some people loose sight of the basics of computer science and that usually tends to lead to bad implementations of concepts.

I remember one time a person creating an interface with the action method to create a step-by-step process. I went back an implemented as a linked list style reader. People could use annotations to guide the reader like @PreCondition, @CleanUp,and of course @Action.

Of course no one is perfect, so I wouldn't say that anything on this blog is the end all, be all for implementations.

No comments: