Abstract classes & methods
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Abstract classes & methods
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  All about Indexers [next Lesson]  Synchronous and Asynchronous Execution
Abstract classes & methods
  by: mosessaur

Abstract classes & methods

by: mosessaur

Abstract class is a class that has no direct instances, but whose descendants may have direct instances. There are case i which it is useful to define classes for which the programmer never intends to instantiate any objects; because such classes normally are used as bae-classes in inheritance hierarchies, we call such classes abstract classes These classes cannot be used to instantiate objects; because abstract classes are incomplete. Derived classes called concrete classesmust define the missing pieces.

Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.

In some cases, abstract classes constitute the top few levels of the hierarchy, for Example abstract class Shape with abstract method Draw() has tow derived abstract classe Shape2D & Shape3D inherites the method Draw() & also do not provide any implementation for it. Now we have normal classes Rectangle, Square & Circle inherites from Shape2D, and another group of classes Sphere, Cylinder & Cube inherites from Shape3D. All classes at the bottom of the hierarchy must override the abstract method Draw().

A class is made abstract by declaring it with Keyword abstract.

Example:
public abstract class Shape
{
    //...Class implementation

    public abstract void Draw(int x, int y)
    {
        //this method mustn't be implemented here.
        //If we do implement it, the result is a Syntax Error.
    } 
}


public abstract class Shape2D : Shape
{
    //...Class implementation
    //...you do not have to implement the the method Draw(int x, int y)
}

public class Cricle : Shape2D
{
    //here we should provide an implemetation for Draw(int x, int y)
    public override void Draw(int x, int y)
    {
        //must do some work here
    }

}
Difference between an abstract method & virtual method:

Virtual method has an implementation & provide the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.

important Notes:

(a)Any Class with abstract method or property in it must be declared abstract

(b)Attempting to instantiate an object of an abstract class retults in a compilation error

Example:

Shape m_MyShape = new Shape(); //it is Wrong to that.
But we can do that.
Shape m_MyShape = new Circle(); // True
Or
Shape m_MyShape; 
/*

declare refrences only, and the refrences can refer to intances of
any concrete classes derived from abstract class
*/

Circle m_MyCircle = new Circle();
m_MyShape  = m_MyCircle; // Also True
(d)An abstract class can have instance data and non-abstract methods -including constructors-.

Tutorial

Our Tutorial has 4 classes { Shape --> Point --> Cirlce --> Cylinder } Abstract Class Shape has 2 virtual methods Area() & Volume(), and one abstract property ShapeName

Class Point only override the inherited property ShapeName as point do not have any area or volume Also it has to properties X & Y (pntA[X,Y]) for example

Class Cirlce override method Area() & the property ShapeName and doen't override volume as it has no volume. Also Cirlce has some methods to provide its Diameter & Circumference which will be used by class Cylinder.

Class Cylinder override all methods in Class Shape, and use the service methods in Class Circle (Diameter & Circumference) to calculate the Area() & Volume(). Check the project and it is well commented.

Download the code: Click Here

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Abstract classes & methods
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  All about Indexers [next Lesson]  Synchronous and Asynchronous Execution


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
carlos_roc.. 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center