Abstract classes in OOPS

Abstract Classes

  • Abstract classes are defined with the keyword ‘abstract’.
  • They are used in design level.
  • Object cannot be created to abstract classes directly. But object can be created to type of abstract class by calling derived class constructor
Abstract Class
Every animal have its specific type just like Wolf, Gold Fish, Dog etc. So assume as No object for Animal class, but inheriting Animal class to any other type of animal we can get the base behavior of animal in those type of animals.

Ex:

        abstract class A
        {
            public abstract int Add(int a, int b);
        }

        class B : A
        {
            public override int Add(int a, int b)
            {
                return a + b;
            }
        }

        class C
        {
            public C()
            {
                A a = new A();  // it is not possible, because there is no constructor for abstract class.
                A a = new B();  // This is possible, because B is not abstract class, so It has it’s default constructor.
                a.Add(10, 20);  // will return 30.
            }
        }
  • Abstract classes can have abstract methods and properties as well as non abstract members. But a normal class cannot contain abstract members.
  • An abstract member should be implemented in its derived classes.
  • An abstract method or property will not be having it’s definition/implementation in abstract class.
  • It is not mandatory to have at least single abstract member in an abstract class. We can have defined methods only in an abstract class.

Ex:

        public abstract class A
        {
            public string Add(string a, string b)
            {
                return a+b; // A normal method
            }
            public static int Add(int x, int y, int z)
            {
                return x + y + z;
            }
        }

Abstract class can have a normal method, but it cannot be used without inheritance. Because object cannot be created for abstract class. At the same time a static method can be used in abstract class and it can be used without inheritance. Because static members doesn’t need object to invoke.

From the above example, “public string Add(string a, string b)” method need inheritance to be invoked at the same time “Public static int Add(int x, int y, int z)” method can be used directly with class name

Ex:

        public abstract class A
        {
            // Abstract property.
            public abstract string FirstName { get; set; }
            // Abstract method.
            public abstract int Add(int a, int b);
            public string Add(string a, string b)
            {
                return a + b; // A normal method
            }
            public static int Add(int x, int y, int z)
            {
                return x + y + z;
            }
        }

        public class B : A
        {
            private string _FirstName;
            public override string FirstName
            {
                get { return _FirstName; }
                set { _FirstName = value; }
            }
            public override int Add(int a, int b)
            {
                return a + b;
            }
        }
        public class C
        {
            public C()
            {
                A b = new B();

                b.Add("Mathew ", "Markose"); //will return “Mathew Markose” as result
                A.Add(10, 20, 30); // will return 60 as result
            }
        }

Refer for more OOPS Concepts

Leave a Reply

%d bloggers like this: