Monday, 29 February 2016

C#.NET --- ABSTRACT CLASS

ABSTRACT CLASS 

In general a method which is not implemented directly and its is implemented in its derived class is known as virtual method.

in general virtual means ' not real ' or ' not actual '  or which is not existed physically is known as virtual 

why virtual methods ?

for suppose if a class is inherited by several classes, but each derived class have different implementation for a single methods, in that case base is gives a chance to implement in their classes rather writing code in that method(s).

for example if a base class CAR is there and it is inherited by two sub classes they are one is BMW and another one is FORMULA _ONE 

the BMW has right hand side steering and FORMULA_ONE have left hand side steering 
there is a function in CAR class Steering()  so rather to write that method either left hand side or right hand side . it is better to set empty or virtual and implement is derived classes
for right hand side in BMW and left hand side FORMULA_ONE respectively. 

Abstract methods and Abstract classes :

Abstract method is a virtual function created with abstract keyword  i.e., it can implemented in appropriate derived class is known as abstract method 

Abstract class is a class which contain at-least  one abstract method is known as abstract class. A abstract class contains both virtual and non-virtual methods 

rules to write a abstract method or abstract class :


  • An abstract method must be created with abstract key word as functional modifier 
  • An abstract method must be placed in a abstract class only , we cannot write it in non-abstract classes
  • An abstract class contains both abstract methods and non-abstract methods 
  • we cannot create a instance for abstract classes So it must be derived by another class 
  • An abstract method must be override in derived class otherwise it will raises compilation error.
  • An  abstract method must be public type access modifier. 
  • While implementing in derived class it must use "override" keyword.
Program :


Output is :::::::




Interview Questions :

1.Can we need to write virtual keyword for abstract methods ?
ans : No need to virtual keyword ,but Although an abstract method is implicitly also a virtual method, it         cannot have the modifier virtual.

2.Where we implement  abstract methods ?
ans: all abstract methods are not provide their implementation of that method. Instead, non-abstract derived classes are required to provide their own implementation by overriding that method.

3. Can we do inheritance by abstract class 
ans: Yes, we can inheritance by abstract classes from normal classes and also abstract classes 
   
example 1 :
   abstract class A
    {
      public abstract void method1();
      public void method2()
      {
       Console.WriteLine("implemented in class A");
      }
    }
   abstract class B : A
  {
    public abstract void method3();

  }
  public class C : B
{
     public void method1()
      {
       Console.WriteLine("implemented in class C");
      }
       public void method3()
      {
       Console.WriteLine("implemented in class C");
      }
}

Here two abstract classes are available which are inheriting one by another, but it is class B responsibility to implement class A abstract method in its sub class C.

4. Can we write 










No comments:

Post a Comment