Sunday, December 28, 2014

Program to show the use of Properties in inheritance and abstract class

9:15 PM - By azeheruddin khan 0

Explanation: - As shown in the below given program, a child class object can be used to access the property of parent class. The object ‘p’ of Program class can be used to access Parent class property: variable. And an abstract property: var of an abstract Parent class can be implemented in its child class as shown above.

Example:

using System;

namespace PropertiesExample1
{
    class Program
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            Console.WriteLine("value of x is " + child.variable);
            child.variable = 20;
            Console.WriteLine("After changing, value of x is " + child.variable);
            Console.WriteLine();
            Console.WriteLine("value of y is " + child.var);
            child.var = 60;
            Console.WriteLine("After changing, value of y is " + child.var);
            Console.ReadLine();

        }
    }

    public abstract class Parent
    {
        int x = 10;
           public int variable
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
           public abstract int var
           {
               get;
               set;
           }
    }
    public class Child: Parent
    {
        int y = 50;
        public override int var
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }
    }

}


Output:






About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top