Object Oriented Programming

​Object Oriented Programming (OOP) is a programming paradigm used to develop modern applications. OOP allows developers to map real world problems, exactly in the same manner in your desired object oriented programming language, therefore developers can plan their design without any high level technical interpretation. This tutorial provides an introduction to OOP and upcoming series of tutorials provide a brief explanation on the OOP concepts.

This is the time to go through a sample code. There is a veterinary doctor, who can treat various animals. Following code simulates this scenario with a VeterinaryDoctor class and some animals.
public class DoctorDemo {
 public static void main(String[] args) {
  Cat cat = new Cat();
  Dog dog = new Dog();
  VeterinaryDoctor doctor = new VeterinaryDoctor();
  cat.age = 4;
  dog.age = 2;
  doctor.treatCat(cat);
  doctor.treatDog(dog);
 }
}

class VeterinaryDoctor {
 /**
  * Give treatment to a cat.
  * @param cat The cat.
  */
 public void treatCat(Cat cat) {
  if(cat.age < 3) {
   System.out.println("Treatment C1");
  } else {
   System.out.println("Treatment C2");
  }
 }

 /**
  * Give treatment to a dog.
  * @param dog The dog.
  */
 public void treatDog(Dog dog) {
  if(dog.age < 5) {
   System.out.println("Treatment D1");
  } else {
   System.out.println("Treatment D2");
  }
 }
}

class Cat {
 int age;
}

class Dog {
 int age;
}
There are no errors in the logic of this code. You can compile and run this program happily without any errors. However, there are two hidden problems and let's point out them.

Problem 1
Inside the main method we can set a negative value as the age of cat and dog. Technically a negative value is allowed for an integer variable, but conceptually is it possible in the real world? Can an animal have a negative age??? Then how we are going to restrict the age of Cat and Dog to positive values only?


Problem 2
In future some more animal may be introduced into this program and the doctor must be able to give treatment to them as well. For every new animals we need to create a new class and need to modify the main method for object creation. But do we need to modify the doctor class also? Definitely we need to add a new method for the newly introduced animal. In a maintainable and extendable project, developers must be able to add new features without a large amount of modifications.

Object Oriented Design provides solution for these problems. In an object oriented program an object can control its properties. For example, in this case the Cat or Dog can throw an exception if the age is a negative value. OOP programs are easier to maintain and extend than a non-object oriented programs. In other words if the above code is designed using OOP practices, a new animal can be added without touching the VeterinaryDoctor class. There are even more advantages; an object created using OOP concept can be easily used by another applications with a less amount of extra work and the developers do not need to worry about the implementation logic; what they need is just the name of available methods in that object.

There are four fundamental concepts in Object Oriented Programming, which are:

These topics are briefly explained in separate articles and the provided code is enhanced in those articles step by step. The VeterinaryDoctor sample is finalized with a perfect OOP design at the end of these article series.

Note: It is highly recommended to follow the articles in the same order as they are listed above.

Find the source code at Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *