Monday, 22 August 2016

Chapter-8 Abstraction and Virtual







Using Abstract Classes
1)         C# enables you to create abstract classes that are used to provide partial class implementation of an interface.
2)         Abstract classes contain abstract methods, which can be implemented by the derived class.
3)         Polymorphism can be implemented by using abstract classes and virtual functions.
4)         There are certain rules governing the use of an abstraction class:
a)         Cannot create an instance of an abstract class.
b)         Cannot declare an abstract method outside an abstract class.
c)          Cannot be declared sealed.
5)         Abstract methods are methods without any body.
6)         The implementation of an abstract method is done by the derived class.
7)         When a derived class inherits the abstract method form the abstract class, it must override the abstract methods. This requirement is enforced at compile time, and is also called dynamic polymorphism.

The syntax for using the abstract method is as follows:

                                 [access-modifiers] abstract return-type method name (parameters]);

The abstract method is declared by adding the abstract modifier to the method.

Demo of Abstract Class and Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractDemo
{
   abstract class demo
    {
       public abstract void display();//abstract Method donot have body
       //{
       //    Console.WriteLine("This is display()");
       //}
       public void show()
       {
           Console.WriteLine("This is show()");
       }
    }
   class demoChild : demo
   {
       public override void display()// To call the abstract method in the child class we need to override it.
       {
           Console.WriteLine("This is display()");
       }

   }
    class Program
    {
        static void Main(string[] args)
        {
         // demo d = new demo();//Abstract class Object cannot created only reference variable is followed
            demo d=new demoChild();//In this case when have created Reference variable of demo But we have allocated the memory to it with help of Demochild class which is know as Runtime Polymorphism or dynamic polymorphism
            d.display();
            d.show();
            Console.ReadLine();
        }
    }
}




Using Virtual Functions

1)      When you have a function defined in a class which you want to allow to be implemented by the inherited classes, you can use virtual function.
2)      The virtual function could be implemented by the inherited classes in their own way and the call to the method is decided at the run time.

Demo of Virtual Function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VirtualDemo
{
    class Demo
    {
        public virtual void display()
        {
            Console.WriteLine("This Demo Virtual display()");
        }
    }
    class DemoChild :Demo
    {
        public override void display()
        {
            base.display();
            Console.WriteLine("This DemoChild Virtual display()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Demo d = new DemoChild();
            d.display();//This will call Demo Virtual then Demochild Virtual
            Console.ReadLine();
        }
    }
}



Using Sealed Classes 

1)      You could restrict users form inheriting the class by sealing the class using the sealed keyword.
2)      The sealed  keyword tells the compiler that the class is sealed, and therefore, cannot be extended.
The following is an example of a sealed class: 

             sealed class FinalClass
                   {
                          private int x;
                          public void Method1()
                          {
                          }
                              }

A method can also be sealed and in that case the method cannot be overridden.

Demo of Sealed Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SealedDemo
{
    sealed class Demo
    {
        public void display()
        {
            Console.WriteLine("This is display()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Demo d = new Demo();//Sealed is class whose Child class cnt be created by default, all the methods,variables
            //inside the sealed class cannot be inherited
            d.display();
            Console.ReadLine();
        }
    }
}

Using Interfaces 
1)      Interfaces define properties, methods, and events, which are known as the members of the interface.
2)      Interfaces are fully supported by C#.
3)      Interfaces are used when a standard structure of methods is to be followed by the classes, and where classes will implement the functionality.
4)      Interfaces separate the definition of objects from their implementation so that the objects can evolve without the risk of introducing incompatibility in existing applications.
5)      Working with interfaces includes interface declaration and implementation of interface by the classes.
6)      You can declare interfaces using the interface keyword.
7)      Interface statements are public, by default.
8)      You can declare only methods, functions, and properties in interfaces. You cannot declare a variable in interfaces.
9)      Interfaces declare methods, which are implemented by classes. A class can inherit from single class but can implement form multiple interfaces.
Inheriting Interfaces 
1)      A class or a structure that implements interfaces also implements the base interfaces of the inherited interface.
2)      Any Class which implements needs to implement the All the method of an Interface.
3)      Interface inherits the another Interface where class implements Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceDemo
{
    interface Iseries1
    {
        void display();
    }
    interface Iseries2 : Iseries1
    {
        void show();
    }
    class Demo1 :Iseries1
    {
        public void display()
        {
            Console.WriteLine("This display() In demo1 class");
        }
    }
    class Demo2 : Iseries2//Demo2 class needs to implement both method display() & show()
    {
        public void display()
        {
            Console.WriteLine("This display() In demo2 class");
        }
        public void show()
        {
            Console.WriteLine("This show() In demo2 class");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Demo1 d1 = new Demo1();
            d1.display();
            Demo2 d2 = new Demo2();
            d2.display();
            d2.show();
            Console.ReadLine();
        }
    }
}






Next Chapter-9 File Handling



Summary:

1)      An abstract superclass is a conceptual class that does not exist in real world but acts as a base from which other classes inherit properties behavior.
2)      A sealed class tells the compiler that the class cannot be extended further. You cannot derive a class from a sealed class.
3)      An interface defines properties, methods, and events.  The properties, methods, and events that are defined in the interface are known as the members of the interface.

I hope that the functionality of the above attribute is clear to you now. Now you can use this code in your application If you have further question, just drop a line below and I will try to answer you as soon as possible.
      Last but not least, connect with me on Twitter , Facebook , LinkedIn and Google+ for technical updates and articles news. We won’t spam or share your email address as we respect your privacy.



No comments:

Post a Comment