Great way to earn money!

Zone1

Tuesday, February 28, 2012

Some CSHARP Questions Part 2

Explain the difference between a class and a structure

Class:

a. It is reference type.
 
b. Null value can be assigned to a variable in a class
 
c. It can have destructor.
 
d. All variables in classes are by default private.
 
e. Good to be used from architecture view as it provides high flexibility.

Structure:

a. It is value type.
 
b. Null value assignment is not feasible here.
 
c. Cannot have destructor.
 
d. All variables in structures are public.
 
e. Good to be used for simple data structures.




What are virtual, sealed, override, new, and abstract keywords.

The virtual keyword enables a class to be overridden.

If it has to be prevented from being overridden, then the sealed keyword needs to be used.

The override keyword is used to override the virtual method in the base class.

Abstract keyword is used to modify a class, method or property declaration.

You cannot instantiate an abstract class or make calls to an abstract method directly.

An abstract virtual method means that the definition of the method needs to be given in the derived class.


Lets have some examples :

  1. By using virtual and override keywords we can accomplish Method overriding.
  2. The virtual modifier indicates to derived classes that they can override this method.
  3. The override modifier allows a method to override the virtual method of its base class at run-time.
To achieve Run-time polymorphism or Late Binding, we are using Method Overriding concept.

using System;

using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    class baseclass
    {
        public virtual int Add(int a, int b)
        {
            return a + b;
        }
    }
    class subclass : baseclass
    {
        public override int Add(int x, int y)
        {
            int d = 20;
            return base.Add(x,y)+d;
        }
    }
    class subclass2 : baseclass
    {
        public int Add(int x, int y)
        {
            int d = 40;
            return base.Add(x, y) + d;
        }
    }
    class something
    {
        public static void Main(String[] args)

        {
            baseclass baseclsinst = new baseclass();
            Console.WriteLine("Base class Addition::::::::::::" + baseclsinst.Add(23, 23));
            baseclass bscls = new subclass();
            Console.WriteLine("Derived class addition:::::::::" + bscls.Add(34, 54));
            baseclass bscls2 = new subclass2();
            Console.WriteLine("Derived class addition:::::::::" + bscls2.Add(34, 54));
            Console.ReadLine();
        }
    }
}


 
Sealed class is used to define the inheritance level of a class.
 
The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. 

.  A class, which restricts inheritance for security reason is declared, sealed class.
2.  Sealed class is the last class in the hierarchy.
3.  Sealed class can be a derived class but can't be a base class.
4.  A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are restricting it to inherit.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace sealed_class
{
    class Program
    {
        public sealed class BaseClass
        {
            public void Display()
        {
            Console.WriteLine("This is a sealed class which can;t be further inherited");
        }
    }
 
        public class Derived : BaseClass
        {
            // this Derived class can;t inherit BaseClass because it is sealed
        }
   
        static void Main(string[] args)
        {
            BaseClass obj = new BaseClass();
 
            obj.Display();
 
            Console.ReadLine();
        }
    }
}


Sealed method is used to define the overriding level of a virtual method.
 
Sealed keyword is always used with override keyword

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace sealed_method
{
    class Program
    {
        public class BaseClass
        {
           
            public virtual void Display()
            {
                Console.WriteLine("Virtual method");
            }
        }
 
       public class DerivedClass : BaseClass
        {
            // Now the display method have been sealed and can;t be overridden
            public override sealed void Display()
            {
                Console.WriteLine("Sealed method");
            }
        }
 
       //public class ThirdClass : DerivedClass
       //{
 
       //    public override void Display()
       //    {
       //        Console.WriteLine("Here we try again to override display method which is not possible and will give error");
       //    }
       //}
 
        static void Main(string[] args)
        {
 
            DerivedClass ob1 = new DerivedClass();
            ob1.Display();
 
            Console.ReadLine();
        }
    }
}


'abstract' modifier before a class indicates that the class is incomplete and intended to be used only as base class and must be implemented in derived class 

 it cannot be instantiated 

it may contain abstract methods

'Abstract' modifier before a method indicates that the method (or property) does not have implementation

it's implicitly a virtual method

The implementation is provided in the overriding method




Difference between Abstract Method & Virtual Method

A virtual method must have its implementation in the base class and may optionally be overriden in the derived class if some additional functionality is required WHEREAS, Abstract Method has no implementation in the base class; it is implemented by the overriding method in the derived class.

In other words, Virtual method has an implementation & provides the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method. 




Now lets come to overriding and new:



A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses
    using System;
    namespace Polymorphism
    {
        class A
        {
            public void Foo() { Console.WriteLine("A::Foo()"); }
        }
        class B : A {}         class Test
        {
            static void Main(string[] args)
            {
                A a = new A();
                a.Foo();  // output --> "A::Foo()"
                B b = new B();
                b.Foo();  // output --> "A::Foo()"
            }
        }
    }
The method Foo() can be overridden in classes B and C:
    using System;
    namespace Polymorphism
    {
        class A
        {
              public void Foo() { Console.WriteLine("A::Foo()"); }
        }
        class B : A
        {
              public void Foo() { Console.WriteLine("B::Foo()"); }
        }
        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;
                a = new A();
                b = new B();
                a.Foo();  // output --> "A::Foo()"
                b.Foo();  // output --> "B::Foo()"
                a = new B();
                a.Foo();  // output --> "A::Foo()"
            }
        }
    }
The method Foo() is a non-virtual method. C# requires the use of the keyword virtual in order for a method to actually be virtual.  

Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.
    using System;
    namespace Polymorphism
    {
        class A
        {
            public virtual void Foo() { Console.WriteLine("A::Foo()"); }
        }
        class B : A
        {
            public override void Foo() { Console.WriteLine("B::Foo()"); }
        }
        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;
                a = new A();
                b = new B();
                a.Foo();  // output --> "A::Foo()"
                b.Foo();  // output --> "B::Foo()"
                a = new B();
                a.Foo();  // output --> "B::Foo()"
            }
        }
     }

C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:
    using System;
    namespace Polymorphism
    {
        class A
        {
            public void Foo() { Console.WriteLine("A::Foo()"); }
        }
        class B : A
        {
            public new void Foo() { Console.WriteLine("B::Foo()"); }
        }
        class Test
        {
            static void Main(string[] args)
            {
                A a;
                B b;
                a = new A();
                b = new B();
                a.Foo();  // output --> "A::Foo()"
                b.Foo();  // output --> "B::Foo()"
                a = new B();
                a.Foo();  // output --> "A::Foo()"
            }
        }
    }
Combining Method Overriding and Hiding

Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:
            class A
            {
                public void Foo() {}
            }
            class B : A
            {
                public virtual new void Foo() {}
            }
     
A class C can now declare a method Foo() that either overrides or hides Foo() from class B:
            class C : B
            {
                public override void Foo() {}
                // or
                public new void Foo() {}
            }
Only methods in base classes need not override or hide derived methods. All methods in derived classes require to be either defined as new or as override.










No comments:

Post a Comment