Back to basics (or rather: What they all will be laughing at in a century) Part I

You might be aware or even take for granted that the two classes that are being used by most programmers today are both called Object. In Java and in .NET these two are the root of all (evil): all types implemented by genius developers around the world are based upon Object.

 

I don’t know whether you’ve ever tried to explain the difference between a class and its instantiation (an object) But when a class (mind you the most important, omniscient class of all) is called Object this doesn’t get any simpler.

 

But it gets better: as any starting Object Orientation adept should be able to tell you it is very important to place methods in the right level in an inheritance structure. Not to low -> you don’t want to copy code in siblings. Not to high -> to prevent classes having methods that are not really applicable. So let’s take a look at a method of Object that is present in Java as well as in .NET: ToString() or toString()

 

ToString is said to give a string, that is a textual, representation of the object. So for instance: a class called Person (that derives from Object) implements this method so objects of type Person show the name of the particular person. Now I wonder… what made the guru’s decide that ALL objects have a string representation? Wouldn’t be an ToInteger as feasible? Or ToXml? Or ToBinary…

 

Oh yes, I see the use of ToString while debugging but is that a valid reason to ruin an interface? Speaking of interfaces: why not have an IObject instead of an Object…

 

One last remark that has been haunting me for 5 years now: Consider this picture (you can find it in the most respectable .NET books and courses but this one comes from an MSDN article on the Common Type System:

 


 

See how ValueType inherits from Object… isn’t Object a reference type (is it?) then how can Value type be not a reference type? Boxing is real… But all nice inheritance rules (a cow IS an animal) seem to be broken…