Showing posts with label Object Oriented Programming Concepts(OOPS). Show all posts
Showing posts with label Object Oriented Programming Concepts(OOPS). Show all posts

06 December 2013

Sealed class explained

In this programming tutorial let's understand what is a sealed class by going through the below topic on sealed class:

  1. What is sealed class
  2. How to define a sealed class
  3. Sealed classes .NET framework
  4. Singleton design pattern & sealed class

What is sealed class

Inheritance is one of the building block of the OOPS. However sometimes, allowing a class to be inherited doesn't make sense. In such circumstances, we can make the class cannot be inherited. C# .NET, has exposed a keyword, "sealed", to make a class cannot be inherited. So in essence, the class which cannot be inherited are called Sealed class.

How to define a sealed class

public sealed class BaseClass
{
}
As you can see above in the above C# code sample, the keyword "sealed" is used to make the BaseClass as sealed class.

Sealed class .NET framework

string (System.String) is a sealed class.

Making singleton as sealed class

28 May 2013

question on static class

How can you make a class which cannot be inherited and instantiated?

If you don't know the answer for this question upfront, you might come up with 3 guess below:

  1. Creating an abstract class:

    Abstract classes cannot be instantiated. It met the first requirement of the question.

  2. Creating a sealed class:

    Sealed classes cannot be inherited. It met the second requirement of the question.

  3. Making the class both abstract and sealed:
       namespace WhenToUseStaticClass
      {
       public abstract static class FormatHelper { }
      }
    
    When a class marked as abstract and sealed, you will get the below compile time error:

    "an abstract class cannot be sealed or static"

    The above error makes sense.
    1. Why an abstract class cannot be sealed class?

      The reason behind creating an abstract class is that to derive a class from it. But sealed class means it cannot be inherited. Abstract and sealed are contradictory and hence an abstract class cannot be sealed class.

    2. Why an abstract class cannot be the static class?

      Static class members can be used only with class name where as abstract class members cannot accessed outside without a derived class instance. In some way an abstract class and static class are contradictory and hence a class cannot be abstract and static.

As you read, answer 1 & 2 doesn't meet the requirement fully and answer 3 is not possible. Then what is the correct answer to the question?

Make the class as static. Static class cannot be inherited and instantiated

16 March 2013

Interview question on function overloading

Recently I heard an interesting question on function overloading. In this post let's see that question and find the answer.

In one of the previous post explained about what is function overloading. You can read the post to know more on function overloading.

The program showing function overloading interview question:

namespace CSharpInterviewQuestion
{
  class Program
  {
    static void Main(string[] args)
     {
       // Question 1
       Method(new object());

       // Question 2
       Method("Interview Question");

       // Question 3
       bject obj = GetText();
       Method(obj);

       // Question 4
       Method(null);
            
       Console.ReadLine();
    }

    public static object GetText() 
    {
     return "function overloading";
    }

    public static void Method(object obj)
        {
            if (obj == null){ Console.WriteLine("Object is null.");}
            else { Console.WriteLine(obj.GetType().Name); }
        }
        public static void Method(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                Console.WriteLine("String is null.");
            }
            else
            {
                Console.WriteLine(text));
            }
        }
    }
}

Answers for the function overloading interview questions:

  1. Question 1 answer: It's straight forward question. It prints "Object" to the console. In function overloading binding happens at compile time and hence the method which takes the object as parameter is invoked.

  2. Question 2 answer: It's another simple one. It prints "Interview Question" to the console. In function overloading binding happens at compile time and hence the method which takes the string as parameter is invoked.

  3. Question 3 answer: It prints "string" to the console and not "function overloading". At compile time obj is of type object and not string type.

  4. Question 4 answer: It's needs thinking. Both object type and string type variable can be null. When null is passed as parameter value, the more specific type, in this case string parameter method is bounded at the time of compilation. Hence it prints "String is null" to the console.

The actual output as seen in console output:

Object
Interview Question
String
String is null.