Microscopic Discussion in Classes


Summary:

This article is going to take a microscopic view of classes,in terms of Inheritance(I) , Multiple Inheritance(MI) and Circular Base Classes (CBC)

Article:

In the following lines , I am going to magnify three topics :-

1. Logic of Class Inheritance
2. Logic of Multiple Inheritance
3. Circular base classes

A class member can either be a static member belonging to the class or an instance member belonging to the instance (i.e accessible through the object and not the class). The default is non-static !

A class is also called a data structure. It consists of data members like constants, fields & events,and functions members like methods, properties, indexers, operators, constructors, static constructors and destructors.

A class within a class in a nested class. Thus we see that we can place 11 different types of entities in a class (see above para.). It is important to mention here that function members are the only members of a class that contain executable code.

Logic of Class Inheritance

All classes derive from object super class. In other words, object is the mother of all classes.

public class abc : object
{
public static void Main()
{
}
}

If you do  not derive from any class then the C# Compiler automatically adds :object to your class definition! Object , the only class to have this feature is not derived from any class. It is ultimate base class of all classes in C# hierarchy ( can be seen in C# MSDN documentation )

class abc
{
}
class abc2: abc
{
}

Class abc is the base class for abc2. The documentation however calls abc the direct base class of abc2. Thus the base classes of abc2 are abc and object.

public class z1
{
public static void Main()
{
}
}
class a1 : System.Delegate { }
class b1 : System.Enum { }
class c1 : System.Array { }

Output :

'a1' cannot inherit from special class 'System.Delegate'
'b1' cannot inherit from special class 'System.Enum'
'c1' cannot inherit from special class 'System.Array'

What these errors implies is that we cannot derive a class from the above 2 classes as they are special !

Logic of Multiple Inheritance

Even I would like to draw attention to a topic of interest for both java and c++ programmers! Observe the following code and guess the output ?

Public class z1
{
Public
static void Main() { }
}
class a1 { }
class b1 { }
class c1 : a1,b1 { }

Output :

'bb': type in interface list is not an interface

What I want to state is that , in C#, we can only derive from one and only one base class. So the question arises as what do we do , if we would like to derive from more than one classes! Simple , we use "interface". Trust me ! that is a complete different topic and I recommend readers to see chapter 9 of "Inside C# from Microsoft Press by TOM ARCHER"

Circular base classes

Yes, the concept of circular base classes is not valid. Though logically people can come up with such concepts but in C# language, it is certainly not acceptable to play with. See the results of the following code snippet :-

Public class z1
{
Public
static void Main() { }
}
class a1 : b1 { }
class b1 : c1 { }
class c1 : a1 { }

Output :

Circular base class definition between 'c1' and 'a1'

Well, the result shows simply a logical impossibility, as this situation might never result. At least,I admit that I never seen any code like that, haven't even thought about it !

Happy .NETing!


Similar Articles