Dec 28, 2019

ABSTRACT CLASS AND ABSTRACT METHOD


abstract method, abstract class in c++, abstract class c#, abstract class in oop
abstract class and method

ABSTRACT CLASS

            If any class defines abstract method or abstract member in it then the class will be an abstract class. We will use abstract modifier to indicate that that is an abstract class, like this
SYNTAX
            Public abstract class classname
           {
           }

ABSTRACT METHOD

            The method that contain without any method body is called abstract method. It’s necessary to use abstract keyword to declare a method as abstract. The method contain only the declaration of the method like this
SYNTAX
            Public abstract void Method ( );
  No body for the method only declaration.

RULE TO MAKE AN ABSTRACT CLASS.

           1.    Abstract class may contain abstract member and non-abstract members. Abstract member                     should declare with the keyword 'abstract'.
           2.    To access the non-abstract member the abstract member must be implementing in child class.               For implement we use override with the child class.
           3.    If the class only contain abstract member then it’s the responsibly to implement that member               in  child class otherwise the program not compile nor execute.
           4.    If the member declares as abstract then never implement in that current class.

 EXAMPLE

public abstract class Parent
    {
      public void Add(int a, int b )
        {
            Console.WriteLine(a + b);
        }
        public void Sub(int c, int d)
        {
            Console.WriteLine(c - d);
        }
        public abstract void Mul(int e, int f);
        public abstract void Div(int g, int h);
    }
    public class Child : Parent
    {
        public override void Div(int g, int h)
        {
            Console.WriteLine(g / h);
        }
        public override void Mul(int e, int f)
        {
           Console.WriteLine(e * f);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Child c = new Child();
            c.Add(12, 14);
            c.Div(18, 6);
            c.Mul(12, 12);
            c.Sub(12, 10);
            Console.ReadKey();
        }
    }

0 comments:

Post a Comment

Please do not enter any spam link in the comment box