tags:

views:

225

answers:

6

I have seen following type of scenario in some websites..can anybody help me when do we use this type scenario exactly...?

class emp
{

    public void add()
    {
        MessageBox.Show("Emp class");
    }
}

class dept : emp
{
    public void disp()
    {
        MessageBox.Show("dept class");
    }
}

emp ee = new dept();

I just want to know when we create this type of object emp 'ee=new dept()' instead of 'emp ee=new emp()' thanks, siva

+8  A: 

The example above is demonstrating inheritance. Inheritance is an "IS A" relationship, in this case the "dept" IS A "emp", which means that any time your code uses an emp, it should also be able to use a dept object.

Assigning a new dept to ee is demonstrating that a dept is a emp, even though it might add additional functionality, such as the disp method.

Andy White
It is actually "Inheritance Polymorphism" to be very precise! ;-)
Cerebrus
+2  A: 

The process shown here is called Inheritance.

Basically what is being done is that the type of the variable ee is declared as of type emp; this is legal because the type dept has a is-a relationship to emp (saying it out loud, it's, "dept is a type of emp").

You can do this when you want to accept any variable that inherits from emp (as denoted by the class dept : emp declaration) as a parameter of some sort.

Jon Limjap
A: 

You mean inheritance? If that's what you are asking about, you need to get a book on object oriented programming in C#.

There's no reason for the method disp() on dept. You can just go:

emp ee = new dept(); ee.add();

That will invoke the add() method in emp.

Richard Hein
I mean when do we create this type of object..emp ee=new dept()
sivaramakrishna
All the time ... for many, many reasons.
Richard Hein
A: 

This is Clearly Inheritance..

In Inheritance U need to create this object if u want to use both derived class methoda aswell as base class methods..

Hope this was Helpful

Cute
A: 

U need to create this object if u want to use both derived class methoda aswell as base class methods..

Cute
A: 

We do it for runtime polymorphism. When we need to call a derived class method, but which derived class needs to be called depends on runtime based on user input. This is a very simple example:

static void Main(string[] args)
        {
            List<Shape> shapes = new List<Shape>();
            shapes.Add(new Circle());
            shapes.Add(new Square());
            shapes.Add(new Rectangle());

            foreach (Shape s in shapes)
                s.Draw();

            Console.Read();
        }

public class Shape
    {
        public virtual void Draw() { }

    }

    public class Square : Shape
    {
        public override void Draw()
        {
            // Code to draw square
            Console.WriteLine("Drawing a square");
        }
    }

    public class Circle : Shape
    {
        public override void Draw()
        {
            // Code to draw circle
            Console.WriteLine("Drawing a circle");

        }
    }

    public class Rectangle : Shape
    {
        public override void Draw()
        {
            // Code to draw circle
            Console.WriteLine("Drawing a rectangle");

        }
    }

*****Output:
Drawing a circle
Drawing a square
Drawing a rectangle*****

In practical scenario maybe the user determines at run-time which shape he wants to draw. So while implementing, you shd create an object of Shape class, and assign a Circle, Rectangle or Square to it depending on user selection (in a switch or if-else). And when you call Shape.Draw(), it will call the appropriate derived class method.

Rashmi Pandit