Abstract Class in C#

 Abstract Class 


    We can not create an object of an abstract class in C#, so we need to inherit the abstract class using another normal class and then we can use the methods and data members present in the abstract class.
In the abstract classes we should not provide the whole implementation of logic but we should only have the outline of the class and then we can inherit the class to provide the full implementation of the class.
    Abstract classes are declared using abstract keyword as shown below.

    public abstract class Vehicle
   {

    }


    If we need to take a real time example of the abstract class then we can imagine an abstract class as a Vehicle which can provide all the data members like steering, tires, gear etc and then we have a abstract methods in the class which can only have declaration and not the implementation like driving. We can then create a normal classes like Swift, Wagonr, Maruti which are different types of vehicles and those classed can inherit from the main class Vehicle and using the data members of the Vehicle class provide the full implementation of  driving method present in the Vehicle class. As every child class has a different implementation for the driving method hence we can override the abstract method in the child class and gives its own implementation. 

    Please find the  example below:

     public abstract class Vehicle
 
{
            public string steering { get; set;}
            
            public string tyres {get; set;}

            public string gear {get; set;}


            public abstract void driving();

  }

    public class Swift : Vehicle
    
{    
     
        public override void driving()
     {
            // Provide the implementation here
     }    


}

Few important points to remember related to abstract class in C# as below.

  • We need to declare an abstract class in C# using abstract keyword.
  • Abstract class can have abstract as well as non abstract methods.
  • Abstract methods needs to be declared with abstract keyword and they can not have implementation.
  • When we inherit any abstract class then we must implement the methods in the child class which are marked as abstract in abstract class.

So today, we saw how we can use abstract classes in C#, their importance. 

Thank You …..

For any issues related to C#, .NET or for any feedback then please write to me on below mail Id and I will surely get back to you.

Email Id : prson94@gmail.com



OOPs Concept in C# - Polymorphism

 Polymorphism


Polymorphism is one of the important concepts in OOPS, if you break the word into two parts then you will understand its meaning. Poly means many and morph means forms.

So we will be discussing how polymorphism is used in C#. There are basically two main types of polymorphism in C# as below.


  • Method Overloading.
  • Method Overriding.


So first we will understand method Overloading.

Method Overloading:

  In method overloading we have same method name for more than one method in a class but either the number of parameters for the method or data type of method are different. It is important to note that we can not have two methods with same method name, same number of parameters and there datatypes but different return type.

Let us understand the concept with an example.

So suppose if I have a class name as Test and in there I have two methods with the same name as add()
but if you look at those two methods then you will understand that both of them are accepting difference number of parameters.

public class Test 
{
        
      public int add(int a , int b)
     {
            return a + b;
     }    

     public int add(int a, int b, int c)
    {
            return a + b + c;
    }    


}

So when you build your code you will see that you have not encountered any errors even though both the methods are having same name, but if make the number of parameters and there data types same in both the method then you will get a compile time error.


Let us now discuss about method overriding.

Method Overriding:

    Method overriding means we can override implementation of method which is present in the parent class in child class. To override a method first we need to inherit from the parent class and the parent class needs to be a declared with virtual keyword.

    Let us understand this with an example so that you will get an clear idea. So we have a parent class as Parent and child class as Child and we have a method in the parent class named as foo() which is declared with keyword as virtual.

    public class Parent
    {
            public int a;
            public int b; 

            public virtual int foo(int a, int b)
            {    
                  //Implementation of parent foo method
            }
            
    }

    public class Child : Parent
    {
            public override int foo(int a, int a)
            {
                // implementation of child foo method.
            }                
    }


So as shown in the above example if you see we have overridden the implementation of foo() method in the child class and in the child class we can use the parent class data members a & b so it gives us code reusability also and child class can have its own functionality.

So if I need to give an example of method overriding in real life world then think of it as Car and different types of cars, so every car has a ability to move/drive but then each type of car like Swift, WagonR, i10 etc. will have its own type of implementation to get driven but every car have same data members like steering, wheels, gear etc...


So today, we have seen how we can use polymorphism in C#, we will discuss other OOPs concepts and many more things in my next post.


Thank You...…..

For any issue regarding C# or .NET please write to me on below mail id and I will try my best to reply.

Email Id : prson94@gmail.com.



OOPs Concept in C# - Inheritance



There are four main concepts present in OOPs

  • Inheritance 
  • Polymorphism
  • Encapsulation  
  • Abstraction

Today, we will discuss about the first OOPs concepts which is inheritance.

Inheritance

With inheritance we can use the variables or methods of one class into another class. Inheritance is very widely used concept of OOPs in C#. There are mainly two types of inheritance

  • Multiple Inheritance
  • Multi-Level Inheritance

Multiple Inheritance:

In multiple inheritance we can inherit from more than one class e.g. suppose there are three class A, B & C, so A can inherit from B & C. 

e.g.

public class A
{

}

public class B
{

}

public class C : B, A
{

}

Multiple class inheritance is not supported in C#.


Multilevel Inheritance:

In Multilevel inheritance B can inherit from A and C can inherit from B.

e.g.

public class A
{

}

public class B : A
{

}

public class C : B
{

}

Multilevel class inheritance is supported in C#.

With inheritance we can reuse the code of parent class in the base class.

e.g. if we have a parent class and child class then child class can inherit from parent class and use its data variables and methods.

    class Program
    {
            static void Main(string[] args)
            {        
                    child c = new child();
                    c.a = 20;
                    c.b = 10;
                    int result  = c.Addition();

                    Console.WriteLine(result.ToString());

                    result = c.subtraction();

                    Console.WriteLine(result.ToString());

                    Console.ReadLine();
            }
    }

    public class Parent
    {
        public int a;
        public int b;

        public int Addition()
        {
            return a + b;
        }

    }

    public class Child : Parent
    {

        public int subtraction()
        {
            return a - b;
        }

    }

So in the above example child is inheriting from Parent class and even though Child class do not have variable a & b still we can use them in the subtraction method of Child class as they are present in the Parent class.

We have created a object of Child class and we can call the method Addition with Child class object as we have inherited from Parent class in Child class. So by using inheritance we can achieve code re-usability.

The output of the above code will be 30 & 10.

So today, we have seen how we can achieve inheritance in C#, The other OOPs concepts we will discuss in my next post.

Thank You...….


For any issues regarding C# OOPs concepts please write to me on below mail Id and I will surely get back to you.