Artem's blog

July 26, 2009

Why do I need inheritance in OOP? Real-world examples.

Filed under: Software — Tags: , , — Artem @ 8:22 PM

The problem in general is that it is sometimes unclear from books why do we need some particular technology. In this case we are going to discuss why do you need inheritance and where it is used in real-life applications.

Let us at first remind what is inheritance (samples in Java):

public class Pet {
  public void say() { }
}

public class  Dog extends Pet {
  public void say() { System.out.println("I am a dog."); }
}

public class  Cat extends Pet {
  public void say() { System.out.println("I am a cat."); }
}

public class Test{
  public static void main(String[] args) {
    Pet pet1 = new Dog();
    Pet pet2 = new Cat();
    pet1.say();
    pet2.say();
  }
}

This  programm will output:

I am a dog.

I am a cat.

The idea is very simple: despite pet1 and pet2 are of type Pet, pet1 is pointing to object of class Dog and pet is pointing  to object of class Cat.

The common question which is usually raised is: why we don’t have just  Dog pet1 = new Dog(); and Cat pet2 = new Cat(); ? Whe do we need to access it via Pet?

Let me give you some real-world examples where do we need it:

1. Servlets. When we create a servlet we inherit base servlet class and override method  doGet() or doPost()to add our functionality to the servlet. The server (for example Tomcat) have list of our servlet classes deployed, and as soon as it gets request for our servlet it loads our class, create an object and call doGet() or doPost() on it. As soon as server have no idea what classes do we have it address object of our class via variable of type HttpServlet.

2. The similar idea was used in early versions of Struts library.

3. In .NET as well as in Java you override Exception class or one of it’s successors to create exception specific to your application. The system (Java or .NET)  knows only how to work with Exception (and RuntimeException specificly in Java) and works with all your exceptions uniformaly.

 

March 24, 2008

Get rid of your nested if operators!

Filed under: Software — Tags: , — Artem @ 3:18 AM

Every one of you know how it is disappointing that you can not use

switch

operators with strings, doubles, your custom objects, etc. But anyway sometimes it is possible to get rid of your nested ifs if you will use static maps. Let us consider an example: here we need to set JavaBean properties of certain types:

 protected static Object fillTestBean(Object bean) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
  Method[] methods = bean.getClass().getMethods();

  for (Method method : methods) {
   if (RTTIUtils.isSetter(method)) {
    Class type = RTTIUtils.getSetterType(method);
    if (type == String.class)
     RTTIUtils.invokeSetter(bean, method, testStr);
    else if (type == boolean.class)
     RTTIUtils.invokeSetter(bean, method, testBoolean);
    else if (type == byte.class)
     RTTIUtils.invokeSetter(bean, method, testByte);
    else if (type == char.class)
     RTTIUtils.invokeSetter(bean, method, testChar);
    else if (type == short.class)
     RTTIUtils.invokeSetter(bean, method, testShort);
    else if (type == int.class)
     RTTIUtils.invokeSetter(bean, method, testInt);
    else if (type == long.class)
     RTTIUtils.invokeSetter(bean, method, testLong);
    else if (type == float.class)
     RTTIUtils.invokeSetter(bean, method, testFloat);
    else if (type == double.class)
     RTTIUtils.invokeSetter(bean, method, testDouble);
    else if (type == Boolean.class)
     RTTIUtils.invokeSetter(bean, method, testBooleanObj);
    else if (type == Byte.class)
     RTTIUtils.invokeSetter(bean, method, testByteObj);
    else if (type == Character.class)
     RTTIUtils.invokeSetter(bean, method, testCharacterObj);
    else if (type == Short.class)
     RTTIUtils.invokeSetter(bean, method, testShortObj);
    else if (type == Integer.class)
     RTTIUtils.invokeSetter(bean, method, testIntegerObj);
    else if (type == Long.class)
     RTTIUtils.invokeSetter(bean, method, testLongObj);
    else if (type == Float.class)
     RTTIUtils.invokeSetter(bean, method, testFloatObj);
    else if (type == Double.class)
     RTTIUtils.invokeSetter(bean, method, testDoubleObj);
    else if (type == Date.class)
     RTTIUtils.invokeSetter(bean, method, testDate);
   }
  }

  return bean;
 }

(here all variables starting with test are constants)
Seems really annoying to write all this ifs and, also what if we will need to check values? Another function will also need this nested ifs!
In this case I usually do the following (factory method -like solution):

 private static final Map initObjects = new HashMap();
 private static boolean isCosideredType(Class type) {
  return initObjects.containsKey(type);
 }
 static {
  initObjects.put(String.class, testStr);
  initObjects.put(boolean.class, testBoolean);
  initObjects.put(byte.class, testByte);
  initObjects.put(char.class, testChar);
  initObjects.put(short.class, testShort);
  initObjects.put(int.class, testInt);
  initObjects.put(long.class, testLong);
  initObjects.put(float.class, testFloat);
  initObjects.put(double.class, testDouble);
  initObjects.put(Boolean.class, testBooleanObj);
  initObjects.put(Byte.class, testByteObj);
  initObjects.put(Character.class, testCharacterObj);
  initObjects.put(Short.class, testShortObj);
  initObjects.put(Integer.class, testIntegerObj);
  initObjects.put(Long.class, testLongObj);
  initObjects.put(Float.class, testFloatObj);
  initObjects.put(Double.class, testDoubleObj);
  initObjects.put(Date.class, testDate);
 }

 protected static Object fillTestBean(Object bean) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
  Method[] methods = bean.getClass().getMethods();

  for (Method method : methods) {
   if (RTTIUtils.isSetter(method)) {
    Class type = RTTIUtils.getSetterType(method);
    if (isCosideredType(type)) {
     RTTIUtils.invokeSetter(bean, method, initObjects.get(type));
    }
   }
  }

  return bean;
 }
 
 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up

Powered by WordPress