Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, March 18, 2017

What is Abstract class and Abstract function

Abstract Class
If anyone want to become part of someone,you have to follow his methodology,contract.So,Any derived class wants to inherit Abstract class,derived class has to follow the contract of Abstract class.
Abstract class determines the nature of the methods that the derived classes must implement, but does not, itself, provide an implementation of one or more of these methods.
Abstract class write contract for derived class.
Abstract class and child class are tightly coupled as there is contract. Abstract Method
you want some way to ensure that a derived class does override all necessary methods of base class. C#’s solution to this problem is the abstract method.
An abstract method contains no body and is, therefore, not implemented by the base class. Thus, a derived class must override it.

A class that contains one or more abstract methods must also be declared as abstract class.

When a derived class inherits an abstract class, it must implement all of the abstract methods in the base class. If it doesn’t, then the derived class must also be specified as abstract. Thus, the abstract attribute is inherited until such time as a complete implementation is achieved.

Abstract class: contract, some implementation, no instantiation
Class: contract, implementation, instantiation

Difference between Virtual and Abstract method
When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. Derived class may or may not override that method.
But When a method is declared as a Abstract method in a base class then that method is not defined in a base class and derived class must have to override that method. 
So if there is virtual function , there is an inspiration for derived class to follow that method or not but there is Abstract function, There is enforcement for derived class .It have to follow that method.

Abstract is a keyword.We can place before class or before function.
If any class is abstract,it can have abstract and member or normal function but in normal class can not have abstract function.
whenever a class is declared as abstract means class cannot be instantiated means we can not create object of class.
now we can have abstract function. we can not have the body of that function,this function has to override means definition of this function needs to be provided by derived class.

Step 1 -  We have abstract  class "Laptop".in this class,we have abstract method "Brand" and normal method "keyboard".abstract method "Brand" cannot be private otherwise compiler gives error "Virtual or abstract member cannot be private".

abstract class Laptop
    public void Keyboard()
        Console.WriteLine("Keyboard Method: This method provides keyboard");
    public abstract void Brand();


Step 2 - We have a class "Dell" that is inheriting Abstract class "Laptop".It must have to override abstract method 'Brand" otherwise compiler gives error  "Error 1 'Dell' does not implement inherited abstract member 'Laptop.Brand()".
In class Dell,We have also declared new method "LaunchDate".

class Dell : Laptop
    public override void Brand()
        Console.WriteLine("Brand method: Laptop brand is dell");

    public void LaunchDate()
        Console.WriteLine("Launch Date: This laptop was launched on 19-March-2017");


Step 3 - 
class Program
    static void Main(string[] args)
        Dell dell = new Dell();


Step 4 - Output

Points to Note - 

1)If we try to create object of Abstract class,compiler gives this error.
Error 1 Cannot create an instance of the abstract class or interface 'Laptop' 

2)Abstract method Brand cannot be private otherwise compiler gives error "Virtual or abstract member cannot be private".

You may also like these  OOPS Concept.

No comments :

Post a Comment