All About Abstract Classes

xiaoxiao2021-03-06  104

Introduction

Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and do not want anyone to create objects of these class types. You can make use of abstract classes to implement Such Functionality In C # using the model 'Abstract'.

An Abstract Class Means That, No Object of this Class Can Be Instantiated, But Can make Derivations of this.

An Example of An Abstract Class Declaration IS:

Abstract Class Absclass

{

}

ABSTRACT CLASS CAN Contain Either Abstract Methods or Non Abstract Methods. Abstract MEMBERS Do Not Have Any Implementation In The Abstract Class, But The Same Has To Be Provided In Its Derived Class.

An Example of An Abstract Method:

Abstract Class Absclass

{

Public Abstract void AbstractMethod ();

}

Also, Note That An Abstract Class Does Not Mean That It Should Contain Abstract Members. Even we can there is:

Abstract Class Absclass

{

Public void NonabStractMethod ()

{

Console.writeline ("Nonabstract Method");

}

}

A Sample Program That Explains Abstract Classes:

Using system;

Namespace AbstractSample

{

// Creating an Abstract Class Class

Abstract Class Absclass

{

// a non Abstract Method

Public Int AddTwonumBers (int Num1, int Num2)

{

RETURN NUM1 NUM2;

}

// An Abstract Method, To BE

// Overridden in Derived Class

Public Abstract Int MultiplyTwonumBers (int Num1, int Num2);

}

// a child class of absclass

Class absderived: absclass

{

[Stathread]

Static void main (string [] args)

{

// you can create an

// instance of the derived class

Absderived Calculate = New Absderived ();

Int added = Calculate.addTwonumBers (10, 20); int multiplied = Calculate.multiplyTwonumBers (10, 20);

Console.writeline ("Added: {0},

Multiplied: {1} ", added, multiplied;

}

// Using Override Keyword,

//Mplementing the Abstract Method

// MultiplyTwonumBers

Public Override Int MultiplyTwonumBers (int Num1, int Num2)

{

RETURN NUM1 * NUM2;

}

}

}

In the above sample, you can see that the abstract class absClass contains two methods AddTwoNumbers and MultiplyTwoNumbers. AddTwoNumbers is a non-abstract method which contains implementation and MultiplyTwoNumbers is an abstract method that does not contain implementation.

The class absDerived is derived from absClass and the MultiplyTwoNumbers is implemented on absDerived. Within the Main, an instance (calculate) of the absDerived is created, and calls AddTwoNumbers and MultiplyTwoNumbers. You can derive an abstract class from another abstract class. In that case ............. ..

EXAMPLE

// Abstract Class1

Abstract Class Absclass1

{

Public Abstract Int AddTwonumBers (int Num1, int Num2);

Public Abstract Int MultiplyTwonumBers (int Num1, int Num2);

}

// Abstract Class2

Abstract Class Absclass2: Absclass1

{

// Implementing AddTTwonumBers

Public Override Int AddTwonumBers (int Num1, int Num2)

{

RETURN NUM1 NUM2;

}

}

// derived class from absclass2

Class Absderived: absclass2

{

// Implementing MultiplyTwonumBers

Public Override Int MultiplyTwonumBers (int Num1, int Num2)

{

RETURN NUM1 * NUM2;

}

}

In the above example, absClass1 contains two abstract methods AddTwoNumbers and MultiplyTwoNumbers. The AddTwoNumbers is implemented in the derived class absClass2. The class absDerived is derived from absClass2 and the MultiplyTwoNumbers is implemented there.Abstract properties

FOLLOWING IS AN EXAMPLE OF IMPLEMENTING ABSTRACT Properties in a class.

// Abstract Class with Abstract Properties

Abstract Class Absclass

{

Protected int mynumber;

Public Abstract Int Numbers

{

Get;

SET;

}

}

Class absderived: absclass

{

// Implementing Abstract Properties

Public Override int NumBers

{

get

{

Return mynumber;

}

set

{

MyNumber = Value;

}

}

}

In The Above Example, There Is A Protected Member Declared In The Abstract Class. The Get Properties for the Member Variable MyNumber IS Defined in The Derived Class Absderived.

Important Rules Applied to Abstract Classes

An Abstract Class CANNOT BE A Sea. The Following Declaration is IncorRect.

// incorrect

Abstract Sealed Class Absclass

{

}

Declaration of Abstract Methods Are Only Allowed In Abstract Classes.

An Abstract Method Cannot Be Private.

// incorrect

Private Abstract Int MultiplyTwonumBers ();

The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.

An Abstract Method Cannot Have The Modifier Virtual. Because An Abstract Method Is Implicitly Virtual.

// incorrect

Public Abstract Virtual Int MultiplyTwonumBers ();

An Abstract MEMBER Cannot Be static.

// incorrect

Publpublic Abstract static int multiplytwonumbers ();

Abstract class vs. InterfaceAn abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

AN EXAMPLE OF Interface:

Interface isampleInterface

{

// All Methods Are AutomaticAll Abstract

INT AddNumBers (int Num1, int Num2);

INT MULTIPLYNUMBERS (int Num1, Int Num2);

}

DEFINING An Abstract Class with Abstract MEMBERS HAS The Same Effect To Defining An Interface.

The Members of The Interface Are Public with No Implementation. Abstract Classes Can Have Protected Parts, Static Methods, ETC.

A class can inherit One or More Interfaces, But Only One Abstract Class.

Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.

.

转载请注明原文地址:https://www.9cbs.com/read-124587.html

New Post(0)