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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » 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.Abstract Classes and Methods
2.Bank Account class is abstract since there is no single implementation for Withdrawal
3.Test abstract classTest abstract class
4.Illustrates abstract classes and methodsIllustrates abstract classes and methods
5.Create an abstract classCreate an abstract class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.