Introduction
This might come as a surprise for many, to see me doubting theinformation presented in almost .NET books even MSDN library but as wedelve deeper into this topic, you will find yourself in the samepredicament as I am now.
What is an Object class?
For those who arenew to .NET, according to MSDN library – "Object is the ultimatesuperclass of all classes in the .NET Framework; it is the root of thetype hierarchy."
This is the class declaration of the object class
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class object
All the classes are required to inherit fromthis Object class but they need not declare inheritance from itexplicitly as it is taken care internally. While all classes inheritfrom it, it doesn't inherit from any super class or interface.
So what's the problem?
This I will explain with a help of an example.
Let's define a class called Foo
Class Foo
{
void show() {}
static void main(string[] args)
{
Foo f=new Foo();
}
}
If you have Visual Studio IDE or any othereditor which supports intellisense, when you type f. you will get thefollowing methods
1. Equals
2. GetHashCode
3. ToString
4. GetType
5. show
Even though you have defined only one method show since Foo implicitly inherits from Object it gets the other four methods.Let's now define an interface called IFoo and make Foo class inherit from it.
Interface IFoo
{
void Ishow();
}
class Foo:IFoo
{
void show(){}
void IFoo.Ishow(){}
static void main(string[] args)
{
Foo f=new Foo();
IFoo ifoo=f;
}
}
Since we have inherited from IFoo, we need toimplement the Ishow method. When we type ifoo.show(), we get a compiletime error saying that the interface doesn't contain a definition forshow method. This is correct since an interface is allowed to accessonly methods declared by it even though the object it might bereferring has other methods.
The problem arises now when we typeifoo.equals(f), ideally this should give a compile time error (notruntime error) it is not declared by IFoo interface or none of itssuper interface but it doesn't. Nor does it give a compile time errorfor using any of the methods of Object class.
This is definitely a discrepancy from the factthat an interface is allowed to access only methods declared by it. Aninterface for sure can't inherit from a class so how then can weexplain for the four methods of Object which are available to anyinterface. The only possible solution can be that there must be a superinterface like IObject as declared below:
Interface IObject
{
public virtual bool equals(object);
public virtual int getHashCode();
public Type getType();
public virtual string toString();
}
and the Object class must have inherited fromthis interface and also that this IObject must be the super interfaceof all the interfaces just like Object is to other classes.
Conclusion
This is one of the issues whichMicrosoft should clarify so that the programmers can have a betterunderstanding of the language.
Name:C.Prashanth
Email Id:seeprash@hotmail.com
I am a MSc Software Engg student doing my final semester project atIntel Technologies, India. I am a fanatic follower of Indian cricketteam and try not to miss any match even if it means bunking college andoffice. I love reading War novels and listening to A.R.Rahman music.
No comments:
Post a Comment