Home > DeveloperSection > Articles > Static and Abstract Class

Static and Abstract Class


C# C# 
Ratings:
0 Comment(s)
 2268  View(s)
Rate this:

Static and Abstract Class in C#.Net


Static Class

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. That is, we cannot use the new keyword to create a variable of the class type. We can access the members of a static class by using the class name itself.

Abstract class

We cannot implement an instance of an abstract class; however we can implement methods, fields, and properties in the abstract class that can be used by the child class

Example

private void button1_Click(object sender, EventArgs e)

        {

            int a1, b1,s;

//creating instance of inherited class as object of abstract class

            abs objAbs = new a();

            a1 = 5;

            b1 = 10;

//calling inc() method of static class

            a1 = stat.inc(a1);

//calling sum()

            s=objAbs.sum(a1, b1);

MessageBox.Show("Total no. of time inc called :" + stat.count.ToString());

            MessageBox.Show("Sum: " + s.ToString());

        }

   

Static class

    public static class stat

    {

//creating static int variable

        public static int count=0;

//creating static method

        public static int inc(int x)

        {

            count++;

            x += 10;

            return x;

        }

    }

 

Abstract class

    public abstract class abs

    {

        public abstract int sum(int x, int y);

    }

 

//class inheriting abstract class

    public class a : abs

    {

//overriding abstract method of inherited abstract class

        public override int sum(int x, int y)

        {

            y=stat.inc(y);

            return (x + y);

        }

    }

Screen shots

 


Don't want to miss updates? Please click the below button!

Follow MindStick