Saturday, July 21, 2018

Abstract class


What is Abstract class?.

The main Purpose of used abstract class is to provide default functionality of its subclassses.

when be declare abstract method in a base class the ever derived class must be declare own its definition.

Points to remember.


  • * it is madatory to override abstract method in a derived class.
  • * we can not create instance of abstrac classs.
  • * but it can be used as parameter in method.
  • Abstract classes are declared using the abstract keyword.
  • We cannot create an object of an abstract class.
  • If you want to use it then it must be inherited in a subclass.
  • An Abstract class contains both abstract and non-abstract methods.
  • The methods inside the abstract class can either have an implementation or no implementation.
  • We can inherit two abstract classes; in this case the base class method implementation is optional. 
  • .An Abstract class has only one subclass.
  • Methods inside the abstract class cannot be private(virtual member & abstract class can not be private)
  • If there is at least one method abstract in a class then the class must be abstract.
  • we can create Constructor or property in abstract class. 


Why can't create instance of an abstract class in C# 

No, you cannot create an instance of an abstract class because it does not have a complete implementation. 

 The purpose of an abstract class is to function as a base for subclasses. 
 It acts like a template, or an empty or partially empty structure,
 you should extend it and build on it before you can use it.



PERFORMENCE: abstract classes with virtual methods have better performance than interface implementation in the .NET Framework 4.0.

Note:1. Virtual and abstract member can not be private.
         2.  An abstract class cannot be a sealed class
         3 .Declaration of abstract methods are only allowed in abstract classes.
         4 .An abstract class can have abstract members as well non abstract members.
         5. We can create property in abstract class.


Example:

public abstract class AbsA
    {
        public abstract void MethodTest();
    }

    class TestB:AbsA {

        public override void MethodTest() {
            Console.WriteLine("TestA");
        }

    }










No comments:

Post a Comment