-1

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

sivaramakrishna
  • 666
  • 3
  • 10
  • 16
  • You need to be much clearer about your question if you want answers that are more specific than a paragraph from an OO tutorial. – mqp May 18 '09 at 05:14
  • This is a duplicate of about 50 questions on StackOverflow, for instance: http://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading and http://stackoverflow.com/questions/778888/which-oo-concept-is-this-an-example-of and one that was asked recently (though I can't find it). Voting to close. – Cerebrus May 18 '09 at 06:28

6 Answers6

8

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
  • 86,444
  • 48
  • 176
  • 211
2

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
  • 94,284
  • 15
  • 101
  • 152
0

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 Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
0

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
  • 13,643
  • 36
  • 96
  • 112
0

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

Cute
  • 13,643
  • 36
  • 96
  • 112
0

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
  • 23,230
  • 17
  • 71
  • 111