Thursday, June 12, 2014

Abstract Classess and Abstract Method



**code example**namespace Abstract_Classess
{
    class Program
    {


        abstract class abstract_class
        {
            public abstract void abstract_method(); //public, protected
            public void concrete_method()  //public,private, protected
            {
                //do this and that
                //print "concreate class"
            }
        }

        class abs_derived1 : abstract_class
        {
            void abstract_method()
            {
                //specific functionality one
                //add 1
            }

            void norm_method()
            {

            }
        }
        class abs_derived2 : abstract_class
        {
            void abstract_method()
            {
                //specific functionality two
                //add 5
            }
        }
        static void Main(string[] args)
        {
            abstract_class abs_class = new abstract_class();
            //cannont create an object of an abstract class

            abstract_class abs_der1 = new abs_derived1(); //possible
            abstract_class abs_der2 = new abs_derived2(); //possible

            abs_der1.abstract_method(); //possible
            abs_der1.concrete_method(); //possible
            abs_der1.abstract_method(); //impossible
            /* not possbile since its not declared in the abstract class. 
             * if needs to call, the function header must be in the abstract class as an abstract method.

*if you need to call it, create an object from an abstract_derived caalss and call it like below*/
            abs_derived1 abs_der1_obj = new abs_derived1();
            abs_der1_obj.norm_method(); //possible

            abs_der2.abstract_method();//possible
            abs_der2.concrete_method();//possible
        }
    }
}

1 comment:

  1. Blazor is a recently released single-page app framework for building interactive client-side web apps with .NET. It uses open web standards without plugins or code transpilation and works with all modern web browsers. If you are a freelance net full stack developer, you can get more relevant projects on Eiliana.com, where top clients regularly post IT development projects.

    ReplyDelete