mardi 9 juin 2015

Is object casting a good practise?

Object oriented programming languages e.g. java, c#, ... support object typecasting. For example this is perfectly valid in java:

URL url = new URL("url");    
URLConnection conn = url.openConnection();

if( !conn instanceof HttpURLConnection )
  throw new Exception("not http request");

HttpURLConnection con = (HttpURLConnection) conn;   

Or another basic example which I tried:

public class Base
{
  public void base(){}
}

public class Derived extends Base
{
  public void derived(){}
}

Base b = new Derived();
b.base();

Derived class has all the methods base class has, plus more. There is no reason why you can’t create base class by calling derived class constructor.

I also came across this link http://ift.tt/1KmdC0l which explains how object typecasting works internally. So far, ok.

But why the first example doesn't use HttpURLConnection con = new HttpURLConnection("url address") (I know HttpURLConnection is an abstract class). It just seems more clear, more simple. On the other hand when you are dealing with interfaces, object typecasting comes in handy. Another example is List<Object> list, which I see sometimes in some classes. It means that you can save every possible class in this list. Afterwards you can just cast it to its original, if you know what type it is. Wouldn't it be more clear to save only specific classes to list i.e. List<String>, List<MyClass>. Is using List<Object> good design practise then at all?

Aucun commentaire:

Enregistrer un commentaire