Demostrates the use of an abstract class, including an abstract method and abstract properties : Abstract Class « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Abstract ClassScreenshots 
Demostrates the use of an abstract class, including an abstract method and abstract properties
Demostrates the use of an abstract class, including an abstract method and abstract properties

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Abstract.cs -- Demostrates the use of an abstract class, including
//                an abstract method and abstract properties.
//
//                Compile this program with the following command line:
//                    C:>csc Abstract.cs
//
namespace nsAbstract
{
    using System;
    
    public class AbstractclsMain
    {
        static public void Main ()
        {
            // Create an instance of the derived class.
            clsDerived derived = new clsDerived (3.14159);
            // Calling GetAbstract() actually calls the public method in the
            // base class. There is no GetAbstract() in the derived class.
            derived.GetAbstract();
        }
    }

    // Declare an abstract class

    abstract class clsBase
    {
        // Declare an abstract method. Note the semicolon to end the declaration
        abstract public void Describe();

        // Declare an abstract property that has only a get accessor. 
        // Note that you
        // do not prove the braces for the accessor
        abstract public double DoubleProp
        {
            get;
        }
        // Declare an abstract property that has only a set accessor.
        abstract public int IntProp
        {
            set;
        }
        // Declare an abstract propety that has both get and set accessors. Note
        // that neither the get or set accessor may have a body.
        abstract public string StringProp
        {
            get;
            set;
        }
        // Declare a method that will access the abstract members.
        public void GetAbstract ()
        {
            // Get the DoubleProp, which will be in the derived class.
            Console.WriteLine ("DoubleProp = " + DoubleProp);
            // You can only set the IntProp value. The storage is in the
            // derived class.
            IntProp = 42;

            // Set the StringProp value
            StringProp = "StringProperty actually is stored in " +
                         "the derived class.";
            // Now show StringProp
            Console.WriteLine (StringProp);
            
            // Finally, call the abstract method
            Describe ();
        }
    }
    // Derive a class from clsBase. You must implement the abstract members
    class clsDerived : clsBase
    {
        // Declare a constructor to set the DoubleProp member
         public clsDerived (double val)
         {
             m_Double = val;
         }
        // When you implement an abstract member in a derived class, you may not
        // change the type or access level.
        override public void Describe()
        {
            Console.WriteLine ("You called Describe() from the base " +
                               "class but the code body is in the \r\n" +
                               "derived class");
            Console.WriteLine ("m_Int = " + m_Int);
        }

        // Implement the DoubleProp property. This is where you provide a body
        // for the accessors.
        override public double DoubleProp
        {
            get {return (m_Double);}
        }
        // Implement the set accessor for IntProp.
        override public int IntProp
        {
            set {m_Int = value;}
        }
        
        // Implement StringProp, providing a body for both the get
        // and set accessors.
        override public string StringProp
        {
            get {return (m_String);}
            set {m_String = value;}
        }
        
        // Declare fields to support the properties.
        private double m_Double;
        private int m_Int;
        private string m_String;
    }
}


           
       
Related examples in the same category
1. Test abstract classTest abstract class
2. Illustrates abstract classes and methodsIllustrates abstract classes and methods
3. Create an abstract classCreate an abstract class
w__w__w.__j_av__a___2_s__.__c_o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.