Object Oriented Programming Refresher

Recently I was challenged by a colleague who asked some very basic questions relating to object orient programming. I know these concepts very well, but sometimes you have to stop and think twice about them, especially if most often than not, you do not implement these concepts in your day to day programming activities. As developers, we often find ourselves in situations where the need to get things out the door and into production tend to encourage us to not think about doing things the right way, but instead, getting the work done in the least possible time.  Object Oriented concepts are something every serious developer should know inside out. Sure you can get by without practicing it, but that approach might come back to haunt you down the road.

Classes are the foundation of Object Oriented Programming. Business Logic code should be seperated into groups of related classes that satisfy a business need. There should be a clear seperation of concerns. When planning a development strategy for our applications, we should at all times try to envision our applications as being seperated into three logical tiers. Strictly speaking, having three logical tiers doesnt dictate that our apps must to be deployed physically as a three tier app...all the tiers could live on one one physical machine, or they could be deployed accross three different machines. They could also be deployed on multiple machines in the enterprise, in what is known as an n-tier architecture.  Our apps will perform much better when we distribute the processing requirements accross multiple machines...business logic components run on machine A, Data Access components run seperate machine, and the front end logic running on a clinet pc or web server somewhere else. Microsft Transaction Server, and now Enterpise Services can act as a broker for our components living in the data access and business logic tiers, and offer such services as transactions support, object pooling, which promotes better scaling applications.

So, what is an abstract class. What is a sealed class. What is the difference accessibility methods out there that I can apply to my classes and methods, and why do I need them? What are interfaces? when is it useful to use an interface? These are the questions that rolled through my head when I was first introduced to OOP years ago. Over time, as you develop more business level applications, the importance of these concepts become clearer.

Inheritance is a pupular term among the OOP purists. So what is Inheritance, and how does it relate to OOP? Perhaps the best way to describe this would be to imagine a hypothetical situation where you are building an app that requires you to Mainatin a list of employee records. All employees must have an employeeID, they must have a first name, and they must have a departmentID etc. In addition, there are some employees that work on a part-time basis, and some that work full-time.

Looking at the above scenario, we have indentified the need for an employee class to service our application. but before we start building our employee class, we need to take a closer look at how that class will be implemented. There are common features that all employees have, but there are also some features that are specific to  the full-time employee and others that are specific to the part-time employee. For instance, when we calculate the monthly pay for the full-time employee, the logic might be different from what we do when we calculate the salary of the guy that is employed part-time.  It might help if we define a baseline Employee class that contain the common functionality, properties etc, and then further extend these classes in other derived classes to inplement the specific functionality. So at the root, we might create an employee class,  and two derived classes: FullTimeEmployee and ParttimeEmployee. Both these classes will be based on the base Employee class and will inherit any functionality exposed, but will further provide indiviual methods that will be used to calculate an employee's salary.

By marking our employee class as Abstract, we are marking it as a class that cannot be instantiated directly...we are saying that this class exists only to be further refined through inheritance. The exact opposite scenario to that would be to mark our class as sealed, which would mean it cannot be further refined through inheritance.

The definition for our base employee class might look like this:

public abstract class Employee
{
}

The definiton for the derived classes might look like so:

public
sealed class PartTimeEmployee:Employee
{}

public sealed class FullTimeEmployee:Employee
{}


Selected data and function members defined in the Employee class will be avaialable in both the derived classes. Data Members such as firstname, lastname, deptID, employeeID are common to all employees are defined on the Employee base class. The Employee base class will also contain a method called CalculateWages, which will contain no functionality, but will tell any class that implements the Employee class that it must implement a CalculateWages method. In the base class, the signature for the calculateWages Method might look like this: 

public
abstract decimal CalculateWages();

The CalculateWage() method in the base Employee class could alternatively be decorated with the virtual modifier instead of abstract. The difference is that the virtual modifier allows the base class to put implementation code in the method, and allows the derived class to decide wether or not it wants to override the implemementation provided by the base class. If the derived class wants to overide the method in the base class, it would implement the method using the following signature:

public override decimal CalculateWages()
{}

The scenario looked at above describes a very simple example of how we might implement a simple employee class in one of our applications. Of course, there is much more to object oriented programming, to much to be described in a single blog post. Of course, there are many different ways to implement OOP...for instance, the employee base class described above could be implemented as an interface instead of as an abstract class. 

In Part 2 of this 4 part series, I'll take a look at other class modifiers, and also some of the accessibility options that we have available for exposing our classes to the outside world and what they mean for our classes. I'll also take a look at Interfaces, and how they can be used in the Employee list management scenario described in this article.

11 Comments

  • Nice short post Jaycent. Any thoughts on briding the gap of OOAD to the service oriented world (SOAD?) (where and object doesn't really exist; state is seperated from function)?

  • Thanks for your feedback Steve. In the world of SOA, the extra layer of abstraction provided shields consuming applications from having to know the intimate details of how the core core components are implemented (via the objects created from the classes). The services layer manages object isntantiation and and object management via publicly exposed methods to which the calling application subscribe to in order to perform work. Object in the services tier are typically stateless, and accepts data via method parameters and normally return data via serializable objects (typically via XML)

  • I think your example about the abstract class for Employee isn't really the right choice. Better to use an interface for that.
    Keep up the writing!

  • Ramon. Thanks for your input. Interfaces will be discussed in a follow-up article. This particlular article does not attempt to describe scenarios where interfaces might be a better choice over abstract classes, the intent was just to look at how to define and consume an abstract class.

  • "all significant pieces of programming logic should be placed inside classes, instead of being placed inside the code-behind files of our web forms on Win forms applications"

    I see what you mean, but web forms (including their code behind files) are classes and the should contain "significant pieces of programming logic". There should however always be separation of concerns and a web form class is a UI class and should contain logic regarding the view, it should not contain any business logic or data access logic. I guess this is what you mean but I just wanted to clarify.

  • Correct Patrik. Code-behind classes and webform classes should contain code that tie the UI logic to the BLL or services tier. Utility functions such as ConverttoFriendlyDate(DateTime mydate) doesnt belong in your code-behind class, its better suited to a utility class that exists on its own right either within the current assembly, or an externally referenced one.

  • economics orbital non business cause growing proxy until

  • Gratitude is the sign of noble souls.

    -----------------------------------

  • -----------------------------------------------------------
    "Advantageously, the publish is really the best on this precious topic. I match in with your conclusions and can thirstily seem forward to your incoming updates."

  • "I was wondering when you can be interested in changing into a guest poster on my blog? and in trade you could put a hyperlink the publish? Please let me know whenever you get a probability and I'll deliver you my contact details - thanks. Anyway, in my language, you'll find not very much great source like this."

  • Virtual methods and abstract classes.. Huh, really? :)

Comments have been disabled for this content.