Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Apr 22 2009

Differentiating OO and Structured Code

Published by Prajwal Tuladhar under Patterns

I know we have have hearing, learning and reading so many things about OO and procedural code from the day we are onto the world of OO. But I just figured out one nice point differentiating between them found in the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin aka Uncle Bob.

Procedural code (code using data structures) makes it easy to add new features without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing features.

I think above single statement is more than enough if one wants to know the real difference between OO and procedural code. And again you gotta to have some OO experience in order to understand the depth of the statement.

So far, I have been satisfied with the book (completed around 10 chapters) and if you are interested in making your code clean (in the sense of usability and design), then I strongly recommend the book.


Comments

Oct 10 2008

Abstract Class versus Interface

Published by Prajwal Tuladhar under Patterns, Programming

In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declared subtype.
Interface generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer. Because interfaces are a form of indirection, some additional overhead is incurred versus direct communication. -Wikipedia

In Java, .NET and PHP 5 there are three ways to create and object i.e. inheritance, composition and interface.

  • Inheritance defines “is-a” relationship. Example: Student and Programmer is a Person
  • Composition defines “has-a” relationship. Example: Class Student contains class Book
  • Interface only models the behavior of an object. Example: Student and Programmer are both Nameable and both may have actions.

In Java, .NET and PHP 5, multiple inheritance (child class with more than one parent class) is not allowed so, interface can be used as a powerful tool to separate implementation. Interface is not a strict “is a” relationship. Abstract class represents some sort of implementation and it is a strict “is a” relationship. For example: A dog is a mammal and a reptile is not a mammal showing strict relationship whereas both dog and reptile may be nameable and both may have some actions.

abstract interface example

As we know that interface and abstract class both provide abstract methods so, making the best use of them is crucial in the object oriented paradigm. Abstract class provides both concrete and abstract methods whereas interface provides only abstract methods. Lets go through an example:

Assume that we have an abstract class Person and its concrete implementation class Student and Programmer. We have an interface called IWork that may or may not be implemented by the concrete classes Student and Programmer. We also have a composite class Address, though it is not required while distinguishing between interface and abstract class, using composite class will help us to clarify the scenario.

Class Diagram

Abstract Interface Class Diagram

Abstract Interface Class Diagram

C# Code

Abstract Class - Person

Abstract Class - Person

Interface - IWork

Interface - IWork

Class Address

Class - Address

Class - Programmer

Class - Programmer

Class - Program

Class - Program

Conclusion

  • Classes in a strict inheritance relationship must be related.
  • Interfaces can be used for classes that are not related. In the above example, only Programmer class is implemented but it is not necessary that Student also implements the IWork interface.
  • An interface never provides any implementation, only behavior.

References

You can download the example from here.


Comments

RSS Feed
Subscribe by email
Follow me @ Twitter