Thursday, September 30, 2010

Abstract and Sealed concepts

Abstract classes and members

Abstract keyword used to define class and class members for the purpose of inheritance.
An abstract class can't be instantiated and an abstract method doesn't have definition in base class, declaration followed by semicolon.

public abstract classA
{
public abstract void dowork(int i); //Abstract method has only declaration in base class
}

A non abstract derived class of an abstract base class must define all the abstract methods of base class and inherited abstract members from upper hierarchy of base class.
public classB : classA
{
public override void dowork(int i)
{
//Implementation of abstract method of base class
}
}

A. Should not define public or protected internal constructor.
B. Can be define Protected or internal constructor.
C. Abstract members are implicitly Virtual.
D. Declaring an abstract member as Virtual or Static is an error.
E. Abstract class may contain non abstract members, non abstract members can have definition in abstract class, meant common method for all derived classes without overriding the method.
F. An abstract derived class can define or not define abstract members of base class.
G. An abstract class can't be declared as Sealed, because having opposite meaning.
H. Non abstract class can't contain abstract members, abstract method can be declared in Abstract class.
I.  An abstract method declaration don't have braces {}, declaration followed by semicolon only.
    Ex.  public abstract void dowork(int i);
J. An Abstract class must implement all members of inherited Interface but not required to override all abstract members of inherited base class.
k. Abstract method declaration is allowed to override a virtual method of base class.

class A
{
   public virtual void F() {
      Console.WriteLine("A.F");
   }
}
abstract class B: A
{
   public abstract override void F();
}
class C: B
{
   public override void F() {
      Console.WriteLine("C.F");
   }
}

Purpose of abstract class to make common definition of a base class that multiple derived classes can share.
A class library has a abstract class than programmers can use the library and have their own implementation of derived class using base abstract class.

Ex.
     public abstract class Animals
    {
        public void Move()  //Non Abstract function
        {
            string s = "All animals can move";
        }
        public abstract void Breath();   
    }
    public class Dogs : Animals
    {
        public Dogs() { }
        public override void Breath()
        {  //Breath in open air
        }
    }
    public class Fish : Animals
    {
        public Fish() { }
        public override void Breath()
        {  //Breath in water
        }
    }
Dogs DaburMan = new Dogs();
            DaburMan.Breath();   //Abstract function of base abstract class
            DaburMan.Move();     //Non Abstract function of base abstract class
       Fish Shark = new Fish(); 
            Shark.Breath();  //Abstract function of base abstract class
            Shark.Move();    //Non Abstract function of base abstract class

***********************

Sealed classes and members


A sealed class can't be inherited, Sealed keyword is used to protect from derivation a class.
A sealed class can't be abstract, because of opposite meaning.
A sealed method also can't be override in derived class.
When applied to method or property sealed keyword always used with override keyword.

If you override an base class virtual method in derived class, you can declare that sealed there(on override) to protect override of that method in further derived classes. This negates the virtual aspect of the member for any further derived class.

public class D : C
{
public sealed override void DoWork()
{
// Implementation
}
}

Scenario - Creating Sealed class
1. To take away inheritance feature from users so they can't inherit that class.
2. If we have some internal operation/s in class or function, because access of class or function in this case can be harmful. ex. .net's string class is sealed.
3. If we have static members in class(Static members are sealed by default). ex pen, brush classes are sealed.

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...