Sunday, June 7, 2015

Object Oriented Programmings: Prevent/Allow Inheritance and Prevent/Allow Instantiate

Hello friend,

In this post I am explaining about 4 scenarios which might occur in any real time project or required while implementing an application. Coding is done in C#. but I am sure similar concepts are available in other languages as well like Java.

  1. Implement class which is allowed to inherit and allowed to create instance
  2. Implement class which is allowed to inherit but not allowed to create instance
  3. Implement class which is not allowed to inherit and allowed to create instance
  4. Implement class which is not allowed to inherit and not allowed to create instance

1. Implement class which is allowed to inherit and allowed to create instance

This is the simplest implementation, just create a public class and it will be allowed to inherited and also allowed to create object of this type of class.
    class Class1
    {
        public virtual void Method1()
        {
            Console.WriteLine("In class 1");
        }
    }
    class Class2 : Class1
    {
        public override void Method1() {
            Console.WriteLine("Method1 In class 2");
        }
        public void Method2()
        {
            Console.WriteLine("In class 2");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class2 c2 = new Class2();
            c2.Method1();   // Print: Method1 in class 2
        }
    }

2. Implement class which is allowed to inherit but not allowed to create instances

To prevent instances of a class is very common feature required for any application. I would like to add one real scenario here in which there is requirement to prevent creating object of class. Suppose we want create one application where we manage employee data for an organization and this organization has 2 types of employee, first Permanent type and second Contract type. There are many properties and behaviors which are common in both type of employees, so we will create one base class to write all the properties and methods. Now if we created this base class with just simple public access modifier, then any one (developer) can create instance of this base class, while conceptually developer should only allow to create object of Permanent employee class or Contract employee class because base employee is not a real employee (there is no existence of only employee in real world, an employee is Permanent employee or Contract employee in this organization1). So to prevent developer creating object of base Employee class, we can use concept of abstract class. Abstract class can not be instantiated, but can be inherited. All the abstract methods of abstract class must be overridden by derived class. Abstract class can also have methods which are not not abstract and full body declaration. If we try to create object of abstract class, compiler will show error as shown below:
We can inherit abstract class easily and implement abstract methods in derived class, as shown below:
abstract class Class1
    {
        public abstract void Method1();
    }
    class Class2 : Class1
    {
        public override void Method1() {
            Console.WriteLine("Abstract method body declaration");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class2();
            c1.Method1(); // Print: Abstract method body declaration
            Console.Read();
        }
    }
Interface also prevent instantiate, you can not create object of an interface. The interface methods must be overridden while abstract class methods can be overridden.

3. Implement class which is not allowed to inherit and allowed to create instance

C# provide sealed modifier which prevent any class to inherit. You will get error if try to inherit sealed class as shown below:
Below code shows how to use sealed modifier and we can create object of sealed type class easily:
    sealed class Class1
    {
        public void Method1()
        {
            Console.WriteLine("In class1");
        }
    }
    class Class2
    {
        public void Method1()
        {
            Console.WriteLine("In Class2");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.Method1();
            Console.Read();
        }
    }
Java provide final modifier to prevent inheritance.

4. Implement class which is not allowed to inherit and not allowed to create instance

To prevent inheritance and instantiate both we can use private constructor of that class. If class constructor access modifier is private then it can not be instantiate as well as inherit from any class as shown below:
Private constructor is used in many application, for example we want to create Utility class and methods of this class should be available directly without creating object of this class, below code shows how to do this:
class Class1
    {
        private Class1()
        {
        }
        public static void Method1()
        {
            Console.Write("class1 static method");
        }
    }
    class Class2
    {
        public void Method1()
        {
            Console.Write("In Class2");
            Class1.Method1();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class2 c2 = new Class2();
            c2.Method1(); // Print: In Class2 class1 static method
            Console.Read();
        }
    }
Please feel free to give any suggestion if I have written any thing incorrect.


No comments:

Post a Comment