Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

03 October, 2013

Common OOP Interview Questions

  1.     .  What is object-oriented programming (OOP)?
    2.       What is class? Give a real life example.
    3.       What is an object? Give a real life example.
    4.       Explain the basic features of OOPs.
    5.       What is the relationship between a class and an object?
    6.       What is difference between OOP and procedural Language?
    7.       What is encapsulation? Why encapsulation is necessary, explain with example?
    8.       Tell about excess modifier.
    9.       What is value type and reference type, explain with example?
    10.   What is method overloading, explain with example?
    11.   What is method overriding, explain with example?
    12.   What is polymorphism, explain with example?
    13.   What is run time polymorphism, explain with example?
    14.   How is method overriding different from method overloading?
    15.   What is inheritance, explain with example?
    16.   What is interface? Why interface is used?
    17.   What is abstract class? Why abstract class is used?
    18.   What is static member? Why static member? How static member is accessed?
    19.   Can you specify the accessibility modifier for methods inside the interface?
    20.   What is constructor? Why constructor is used?
    21.   What is constructor overloading?
    22.   Is it possible for a class to inherit the constructor of its base class?
    23.   Is it possible for a class to inherit the constructor of its base class?
    24.   What is the difference between arrays and collection?
    25.   What are collections and generics?
    26.   How can you prevent your class to be inherited further?
    27.   What is the index value of the first element in an array?

10 December, 2012

Open Close Principle


Open Close Principle is an Object Oriented Design principle. It is first introduced by Betrand Meyer in 1988. He says “Software entities (Class, module, function etc.) should be open for extension, but closed for modification”. An entity is “Open for extension” means that its behavior can be extended to accommodate new demand. The entity is “closed for modification” means that the existing source code of the module is not changed or minimum change when making enhancement. It is clear that if a system cannot accommodate change easily, its life cycle will end fast.

Sometimes code changes introduce heavy risk. At the time of changing, you must ensure that changes will not break the system. Sometimes it takes huge regression testing. This risk can be minimized if no changes are made to existing code.

So, our intention should be writing code in such a way that new functionality should be added with minimum changes or not changes in the existing code.  It should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged. The major advantages of “open close principle” is that it undergo changes and its value will be tremendous. It required almost no regression testing.


Let’s introduce open close principle with an example. Suppose, in our application, we need a “Area calculator” which calculate area of rectangle. However, in this occasion we just create a class AreaCalculator then there will be a method RectangleArea in this class which just calculates area of rectangle. It works fine. In the middle of the application development, we need to calculate area of Triangle and Circle. In this occasion, what should we do? We just add another two method TriangleArea and CircleArea and can do the job. But several problems will arise here – for each new shape you have to add new unit of code. Developer must have to know the logic to calculate area of new shape. Adding a new shape might effect in existing functionalities. So, it will take huge cost of regression testing. This is actually violate, open close principle.

We implement the same problem abide by open close principle by the following way. Here Rectangle, Triangle and Circle class inherit the Shape class and implement CalculateArea Method. In this way, if you need to calculate area of x shape just add a class of x and then implement shape and calculate area of x without modifying exiting code.

Class diagram:


Open Close Principle Implementation by C#:

Step 1: Create abstract Shape class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCP
{
    public abstract class Shape
    {
        public abstract double CalculateArea();
    }
}

Step 2: Create Rectangle class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCP
{
    public class Rectangle : Shape
    {
        public double Height { get; set; }
        public double Width { get; set; }

        public Rectangle(double height, double width)
        {
            this.Height = height;
            this.Width = width;
        }
    
        public override double CalculateArea()
        {
            return Height * Width;
        }
    }
}

Step 3: Create Triangle class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCP
{
    public class Triangle : Shape
    {
        public double Base { get; set; }
        public double Height { get; set; }

        public Triangle(double vbase, double vheight)
        {
            this.Base = vbase;
            this.Height = vheight;
        }

        public override double CalculateArea()
        {
            return 1 / 2.0 * Base * Height;
        }
    }
}


Step 4: Create circle class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCP
{
    public class Circle : Shape
    {
        public double Radius { get; set; }


        public Circle(double radius)
        {
            this.Radius = radius;
        }

        public override double CalculateArea()
        {
            return Math.PI * Radius * Radius;
        }
    }
}


Step 5: Client class which uses Rectangle, Triangle and Circle class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OCP
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Shape objShape = new Rectangle(20, 30);
            Console.WriteLine("Area of Rectangle: " + objShape.CalculateArea());

            objShape = new Triangle(20, 30);
            Console.WriteLine("Area of Triangle: " + objShape.CalculateArea());

            objShape = new Circle(4);
            Console.WriteLine("Area of Circle: " + objShape.CalculateArea());


            Console.ReadKey();
        }
    }
}

05 September, 2012

Procedural VS Object Oriented Programming

Object Oriented and Procedural are two programming paradigm. Procedural programming creates a step by step program that guides the application through a sequence of instructions. Procedural programming is also called linear programming. Generally, code is executed from the top of the file to the bottom in procedural programming. Object Oriented uses object to design applications and computer programs. It isn't as linear as procedural programming. Code is often broken up and distributed across multiple files, each one with a single purpose.


1.    Programming Style: Procedural is linear programming and OOP is not linear programing.

2.  Fundamental Unit: Function is the fundamental unit of procedural programming and object is the fundamental unit of OOP.

3.  Code Organization: In procedural programming code is organized into small procedures. This procedure is used to perform any operation.

In object oriented programming, the data and related functions are bundled together into an object.

4.    Data Orientation: In procedural programming functions have no intrinsic relationship with the data they operate on. Here, you provide the correct number and type of arguments, the function will do its work and returns its output.

In object oriented programming, the data and related functions are bundled together into an "object". Ideally, the data inside an object can only be manipulated by calling the object's functions.

5.    Global and Shared data: We can see that one of the principle differences is that procedural systems make use of shared and global data, while object oriented systems lock their data privately away in objects.

6.   Reusability: Reusability is one of the best features of OOP. Reusability is higher than procedural programming.

7.    Abstraction: Abstraction of OOP is higher than procedural programming so maintenance is easy.

8.  Inheritance: Inheritance is the key of OOP but Procedural programming doesn’t support inheritance.

9. Encapsulation & Polymorphism:  Encapsulation and polymorphism is two key features of OOP which are absent in procedural programming.

    10. Coupling and Cohesion: Loosely couple and maximum cohesion in the system is only possible by OOP but it is not possible by procedural programming.

11.  Cost: OOP takes less cost than Procedural programming.