Monday, November 17, 2008

Why use object oriented programming…2

In my previous post, I’ve written how OOP resolves basic issues such as

• Functional Clutter
• Problem with Global Variables
• Unmanageable associations

Apart from these OOP has other powerful features, namely Inheritance and abstract classes. In this blog I’m going to discuss what these terms mean and why are they so useful.

We all know the common book definition of inheritance, but why is it so useful?
Basically inheritance is used when you have to

1) Modify the behavior of the object
2) Add additional features to the existing class

To modify the behavior all you gotta do is inherit the parent class into a new class called the base class and override the necessary methods. Take the example of JLabel in swing. The text in the label is rendered onto the screen by calling the paint method. Suppose you wanted to change the way border is shown, you have to override this method which is in turn called by swing framework to render the label.

To add additional features to the label, you have to inherit it into the base class and write additional methods for enabling new features. The base class now contains all the previous functionality that lets keeps it compatible with the existing framework.

Inheritance is essentially like copying and pasting all the code within the parent class into the base class. So why not copy and paste the code manually? I.e. copy code in class A to Class B instead of inheriting.

Just imagine what would happen if class A is modified later on as a result of bug fix or enhancement. You’ll have to re-copy all the code from class A to class B, thereby hampering the code manageability. Moreover inheritance lets you reuse the well designed and tested classes you’ve already made.

What’s more, inheritance also helps you debug the code. Suppose class A, B, C, D inherits class E. If there is a problem in classes A to D then it’s probable that the code in class A is buggy. If class A is buggy then it’s probable that the new code you’ve added is buggy. It may seem like a small thing but believe it or not, about 70% of development time is wasted in debugging and maintenance.

Abstract classes…Inherit benefits of Inheritance

Suppose there are a lot of things in common between the two classes (A and B), i.e. you notice some sort of code redundancy, but there are also many marked differences between them at the same time. Furthermore the redundant code cannot be grouped within a single class as it doesn’t make any sense. If you come across this kind of a situation, you could put all that redundant code in an abstract class (X) and make then create the other two by inheriting the abstract class. This kind of model is blessed with the benefits of inheritance. In short it Inherit benefits of Inheritance ?

Abstract classes don’t have an instance, i.e. it’s not possible to create an object without implementing the abstract methods. Simply make the class specific methods, the ones used by classes A and B as abstract. Sounds like Greek and Latin? Let’s consider an example.

Yesterday I was trying to make a class that can add the ‘trail period’ feature to any software. To do this I’ll have to –

1) Record the date when the software is first used.
2) Whenever the software is started, check the current date with the start date and evaluate the difference
3) If the difference is greater than say 30 days then prompt the user to enter the serial key.

To record the start date there can be two possibilities.

1) Save the data in an encrypted form within a file.
2) Save the data in an encrypted form within a database in case of a distributed application.

So you make two classes, DBValidator and NonDBValidator. Both of then contain redundant code…i.e. the logic that checks the date difference, a fraud detection system to see if the user tampers with the system date etc etc…The only point of change is the way in which the start date is persisted onto a storage medium.

In this scenario you could create an abstract class called validator with all the redundant code and make the persistDate method abstract.

Classes DBValidator and NonDNValidator inherit the validate class by implementing the abstract method persistDate, each one defining their own persistence logic.

Why go through this entire headache? Read the inheritance part again. This helps in centralizing the changes that may be required. You just have to make a change at a particular common place (the parent) and the necessary updates are forwarded to all the base classes.

Today’s software development standard is not about writing logical code, anyone can do that today. Instead more emphasis is laid on writing code that is better managed, even if you’ll have to compromise on the logic part. This is probably one the reasons as to why they look for people with work experience. Anyone involved in big projects would’ve certainly faced this problem before. This is also the reason why frameworks such as hibernate, JSF, struts are so popular in spite of the high learning curve involved.

Now that the basics of OOP is laid out, in my next post I’ll discuss about UML diagrams. I hope this article was useful.

No comments:

Post a Comment