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
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.
ReplyDeleteCall Us For Free Demo:- +91 9654271931.
Get Free Demo:- https://ziyyara.in/ad-contact
Visit Us:- Online Tuition In Chandigarh