Skip to main content

Classes in C# Explained

C# Class Explained

A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99.

For instance, if you want to work with Guest’s data as in our previous DataDriven Web application.
The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data.
All of these operations can be represented as a class in C# as shown below.
using System;

namespace CsharpnaijaClassTutorial
{
    public class Guest
    {
        public int Id { get; set; }
        public string GuestName { get; set; }
        public string Address { get; set; }
        public string WhomToSee { get; set; }
        public DateTime DateVisited { get; set; }
        public DateTime TimeVisited { get; set; }

       public void AddGuest(Guest guest)
        {
            //Add guest property to data storage
        }
        public List<Guest> GetGuests()
        {
            return new List<Guest>();
        }
    }
}

The snippets above shows a class with some properties
In this post, we will look at how we can work with classes and their types in C#.

Type of Classes

We have basically the following types of Classes
1.     Abstract Class
2.     Concrete Class and
3.     Sealed Class

Abstract Classes

These are classes marked by the keyword abstract in their class definition, they are typically used to define a base class in the hierarchy. The special aspect of them, is that, you can't create an instance of them - if you try, you will get a compile error. Instead, you have to subclass them as shown below.

    public abstract class Blog
    {
        public int id { get; set; }
        public string Title { get; set; }

    }


As you can see from image above, we are trying to create an object of Blog class that is defined as abstract by using abstract keyword, we received a red underline on new Blog() signifying a compile error.

Now let’s make use of the Blog by defining another class called Csharpnaija that inherits the Blog class.

    public abstract class Blog
    {
        public int id { get; set; }
        public string Title { get; set; }

    }
    public class Csharpnaija:Blog
    {
        public string Author { get; set; }
    }


From the above code, we can now see Csharpnaija inherits Blog as the base class, but added another property called Author. Now Csharpnaija has three properties which include Id, Title and Author and we can now create an instance of the Csharpnaija.


The Csharpnaija has now becomes a concrete class of Blog class
Features Abstract Classes
1.     Use as base class
2.     Cannot be instantiated
3.     Enhance polymorphism
4.     Encourage Inheritance

Concrete Classes

A concrete class in C# is a type of subclass, which implements all the abstract method of its super abstract class which it extends to. It also has implementations of all methods of interfaces it implements. This case, our Csharpnaija class is a concrete class that inherited all properties of Blog class.

Concrete classes have the following features;

1.     can create instance as well as can be inherited
2.     All functionality completed
3.     In application class hierarchy, concrete class need not be in the first level
4.     No method should be abstract

Sealed Classes

These are classes used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, it cannot be inherited. 
The sealed modifier is used to declare a class as sealed in C#, it is the same as NotInheritable in Visual Basic .NET. If a class is derived from a sealed class, compiler throws an error. Structs are sealed. You cannot derive a class from a struct. 

    public sealed class SealedBlog
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
    }


We can create an instance or object of a sealed class, in the case of this post, an object of SealedBlog is instantiated and initialized as shown below.
The output is as shown below

Features of Sealed Classes
1.     Cannot be inherited
2.     take away the inheritance feature

Comments

  1. Are you looking for online tuition in Chandigarh for your child so that your child can complete his/her studies sitting at home without any hassle, then you should take online tutoring from Ziyyara because all the kids studying near Ziyyara always get good marks. Don't delay register today.
    Call Us For Free Demo:- +91 9654271931.
    Get Free Demo:- https://ziyyara.in/ad-contact
    Visit Us:- Online Tuition In Chandigarh

    ReplyDelete

Post a Comment

Popular posts from this blog

Collections in C#

Collections in C# In our previous article , we have learned about how we can use arrays in C#. Arrays in programming are used to group a set of related objects. So one could create an array or a set of Integers, which could be accessed via one variable name. What is Collections in C#? Collections are similar to Arrays, it provides a more flexible way of working with a group of objects. In arrays, you would have noticed that you need to define the number of elements in an array beforehand. This had to be done when the array was declared. But in a collection, you don't need to define the size of the collection beforehand. You can add elements or even remove elements from the collection at any point of time. This article will focus on how we can work with the different collections available in C#. There are three distinct collection types in C#: standard generic concurrent The standard collections are found under the System.Collections. They do not store elemen...

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

System.IO Namesapce in C#

  System.IO Namesapce in C# A  file  is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a  stream . The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the  input stream  and the  output stream . The  input stream  is used for reading data from file (read operation) and the  output stream  is used for writing into the file (write operation). From the above definition of file, the C# provides a namespace that enable us to manipulate file in C# called System.IO.   System.IO  is a  namespace  and it contains a standard IO (input/output) types such as classes , structures , enumerations , and  delegates  to perform a read/write operations on different sources like file, memory, network, etc.   System.IO Classes The table below shows differen...