articles

Home / DeveloperSection / Articles / Static and Abstract Class

Static and Abstract Class

Anonymous User4046 28-Jul-2010
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

Static and Abstract Class

 privatevoid button1_Click(object sender, EventArgs e)
        {
            int a1, b1,s;
//creating instance of inherited class as object of abstract class
            abs objAbs = newa();
            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 Static class


    publicstaticclassstat
    {
//creating static int variable
        publicstaticint count=0;
//creating static method
        publicstaticint inc(int x)
        {
            count++;
            x += 10;
            return x;
        }
    }


 Abstract class


    publicabstractclassabs
    {
        publicabstractint sum(int x, int y);
    }
 
//class inheriting abstract class
    publicclassa : abs
    {
//overriding abstract method of inherited abstract class
        publicoverrideint sum(int x, int y)
        {
            y=stat.inc(y);
            return (x + y);
        }
    }

Screen shots

Static and Abstract Class Static and Abstract Class


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By