Class, Object and Class Inheritance in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 6852 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Namespaces and Using directive in C# before this article.

Introduction

So far, we have discussed about namespaces in C#. Now, let's see the definition of class and class objects in this chapter.

Objective

The main objective of this article is to learn about classes in C#, creating objects to a class and class inheritances in C# programming.

Class

Class is a reference type which enables to create own custom types by grouping the variables of other types, events and functions.
Syntax:
class MyClass
{
    .......;     // class body
    ......;      // class body
}
  • They are declared by using 'class' keyword.
  • 'MyClass' in the above syntax is your class name which follows the base class and interfaces.
  • Class body (with in the braces) can have all the class members such as properties, methods, fields, constructors, nested types, finalizer etc.
  • Class defines the behavior and data of a type.
  • Class supports inheritance (OOPS fundamental).
Creating Objects to a Class
Object is an instance of a class (However both are different). Class defines an object but it cannot be defined as an object.
             
  • To create an object, new keyword is used. For example,
    MyClass obj1 = new MyClass(); // Object 'obj1' for 'MyClass' is created.
    In the above example, we have created an instance (object) to the class (MyClass).
  • After that (creation of object), a reference to that object is passed back to the programmer.
  • In the above example, obj1 is a reference to an object based on MyClass which refers to the new object without any data.
  • We can create reference without creating the object. For example,
    MyClass obj2; // Reference is created but not object
    Note: new keyword is necessary to create an object.
  • We can also refer multiple object references to the same object. For example,
    MyClass obj3 = new MyClass();  // creating reference for new object
    MyClass obj4 = obj3;           // creating reference for the same object.
    MyClass obj5 = obj3;
  • Since objects are referred to the classes by reference, classes are known as reference types.
Class Inheritance
Inheritance is a mechanism of creating a class (child or derived) from another class (parent or base), which inherits the data and behavior from that class (parent).
Inheritance allows the derived (child) classes to overload all the methods from their base (parent) class.
                   
Syntax:
public class MyDerived : MyBase
{
    // All the methods, fields, events, properties etc. are inherited 
    // from 'MyBase' class to 'MyDerived' class
}
In the above example, MyDerived is the derived class which gets all the properties of MyBase.

Note: Constructors cannot be inherited from base class to derived class.

In C#, a class cannot inherit multiple base classes directly. For example,
public class MyDerived : MyBase1, MyBase2 // Not possible
{
    ..........;
    ........;
}

But, a class may inherit multiple base classes indirectly such as in the below figure,
                       
From the above figure,
public class MyBase2 : MyBase1
{
    // Base1 methods will be inherited (not private methods)
}
And,
public class MyDerived : MyBase2
{
    // Base2 methods will be inherited (not private methods)
    // Base1 methods also inherited indirectly through Base2 (not private methods).
}
The above code explains inheriting properties from multiple base classes in C#.

Note: Inheritance doesn't have ability to inherit properties from a 'private' class

Conclusion

In this article, we have seen class in C# with creating objects and class inheritances. Hope you understand.

Thanks for reading.

Regards,
Krishna.

Recommendation
Read Fields, Modifiers allowed by Fields in C#. after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 8/23/2014 | Points: 25
One very important point to mention here is that in C#, a class can't inherit multiple classes directly such as

public class MyClass: Mybase1, Mybase2 // this is not possible
{

}

Also, inheritance gives ability to use methods and properties of the base class only when they are not of private in nature. Such as protected, public etc. methods of base class are accessible but not private methods or properties.
Posted by: Goud.Kv on: 8/25/2014 | Points: 25
Yes, those are much needed points.

Thanks..

Login to post response

Comment using Facebook(Author doesn't get notification)