Tuesday, July 20, 2010

Instanceof operator kills Object Oriented Programming


Target audience: Beginners

Everybody knows about instanceof operator of Java. I just give a simple description about instanceof operator before get into details. The instanceof operator allows a user to check whether an object is an instance of a particular type.The type could be a class or an interface.

if (obj instanceof type) {
type new_name = (type) obj;
}

Here the obj is an instance of the type then, the condition will be true , else it will be false. This condition will also become false if obj is a null. So better to be careful while using it.

I personally feel that sometimes the instanceof operator is missused and it kind of kills the object oriented concept. Let me explain by taking a simple example similar to what we deal with everyday

Suppose we have abstact class Person and two other sub classes Student and Teacher

abstract class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Student extends Person {
void study() {
}
}

class Teacher extends Person {
void teach() {
}
}

Appart from the inherited methods, Student has a method study() and Teacher has a method teach(). Suppose if there is a situation that we have a list of mixed objects of Student and Teacher and we need to call their own methods. We might write a code similar to the following one.

List list = new ArrayList();
list.add(new Teacher());
list.add(new Student());

for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
if (object instanceof Student) {
Student obj = (Student) object;
obj.study();
} else if (object instanceof Teacher) {
Teacher obj = (Teacher) object;
obj.teach();
}
}

This is the way we use the instanceof operator. We can get rid of this if we apply some Object oriented concept.What I'm going to do is, I’m going to change the Person,Student and Teacher classes a bit

abstract class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

abstract void doYourWork();
}

class Student extends Person {

void study() {
}

void doYourWork() {
study();
}
}

class Teacher extends Person {
void teach() {
}

void doYourWork() {
teach();
}
}

I added a abstract method doYourWork() in Person class and let the sub classes Student and Teacher to implement it and call their own methods inside their implementation of doYourWork() method. Now i’m going to change the iterating part also.

List list = new ArrayList();
list.add(new Teacher());
list.add(new Student());

for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Person object = (Person) iterator.next();
object.doYourWork();
}

Wow... the instance of operator is gone and code looks pretty too. I just applied a simple OO concept and got rid of the instanceof operator. This is why i mentioned “I personally feel that sometimes the instanceof operator is missused and it kind of kills the object oriented concept.”

No comments:

Post a Comment