Sunday, September 18, 2011

Override and overload


Que: What is overriding?
Ans: Redefine a method in derived class that is in base class, i.e. same method name in base and derived class with different implementation.

Que: What is difference between override and new keyword?
Ans:  New -> Method is not overridden, it is different in derived.It means if a reference of base type which is pointing at derived type will execute a method of base class, althod same signature is available in derived class.
override-> Indicates that this method is overridden. If a reference of base type is pointing at derived class then method of derived class should get execute.

Que: What is the use of virtual keyword?
Ans: Virtual keyword is to show that this method can be overridden. A method which is not virtual can not be overridden.

Exercise: Find the output of the program.
1)  r1=?,r2=?
public class A
        {
            public A(){}                                   
            public string myMethod()
            {
                return "A";
            }
        }
        public class B:A
        {
            public B():base(){}                                   
            public string myMethod()
            {
                return "B";
            }
        }

static void Main(string[] args)
        {       
            B b1 = new B();
            A a1 = new B();
            string r1 = b1.myMethod();
            string r2 = a1.myMethod();
        }
Ans: r1 = "B"; r2="A"

2) r1=?,r2=?
public class A
        {
            public A(){}                                   
            public virtual string myMethod()
            {
                return "A";
            }
        }
        public class B:A
        {
            public B():base(){}                                   
            public string myMethod()
            {
                return "B";
            }
        }

static void Main(string[] args)
        {       
            B b1 = new B();
            A a1 = new B();
            string r1 = b1.myMethod();
            string r2 = a1.myMethod();
        }


Ans: r1 = "B"; r2="A"

3)r1=?,r2=?
public class A
        {
            public A(){}                                   
            public virtual string myMethod()
            {
                return "A";
            }
        }
        public class B:A
        {
            public B():base(){}                                   
            public override string myMethod()
            {
                return "B";
            }
        }

static void Main(string[] args)
        {       
            B b1 = new B();
            A a1 = new B();
            string r1 = b1.myMethod();
            string r2 = a1.myMethod();
        }
Ans: r1 = "B"; r2="B"

4)r1=?,r2=?
public class A
        {
            public A(){}                                   
            public string myMethod()
            {
                return "A";
            }
        }
        public class B:A
        {
            public B():base(){}                                   
            public override string myMethod()
            {
                return "B";
            }
        }
static void Main(string[] args)
        {       
            B b1 = new B();
            A a1 = new B();
            string r1 = b1.myMethod();
            string r2 = a1.myMethod();
        }

Ans: compile error, cannot override inherited member because it is not marked virtual, abstract, or override.

5) r1,r2,r3,r4?
public class A
        {
            public A(){}
            public string method1()
            {
                return "A->method1";
            }           
        }

        public class B:A
        {
            public B():base(){}          
            public string method2()
            {
                return "B->method2";
            }
        }

static void Main(string[] args)
        {
            A a1 = new A();
            B b1 = new B();
            A ab1 = new B();           
            string r1 = a1.method1();
            string r2 = b1.method1();
            string r3 = ab1.method1();
            string r4 = b1.method2();
            // string r5 = ab1.method2(); -- method2 will not be found in ab1.
        }

Ans: r1= "A->method1" ; r2= "A->method1" ; r3= "A->method1" ; r4= "B->method1"

6)what is the output?
public class A
        {
            public A(){}
            public void method1()
            {
                Console.WriteLine("A->method1");
            }    
        public void method2()
            {
                Console.WriteLine("A->method2");
            }     
        }

        public class B:A
        {
            public B():base(){}         
            public void method2()
            {
                Console.WriteLine("B->method2");
            }
        }

static void Main(string[] args)
        {
            A a1 = new A();
            B b1 = new B();
            A ab1 = new B();          
            a1.method1();
            b1.method1();
            ab1.method1();
            b1.method2();
            ab1.method2();
        }
Ans:
A->method1
A->method1
A->method1
B->method2
A->method2

7)what is the output?
public class A
        {
            public A(){}
            public void method1()
            {
                Console.WriteLine("A->method1");
            }   
        public void method2()
            {
                Console.WriteLine("A->method2");
            }    
        }
        public class B:A
        {
            public B():base(){}        
            public void method2()
            {
                Console.WriteLine("B->method2");
            }
        }

static void Main(string[] args)
        {
            A a1 = new A();
            B b1 = new B();
            A ab1 = new B();         
            a1.method1();
            a2.method2();
            b1.method1();
            b1.method2();
            ab1.method1();          
            ab1.method2();
        }
Ans:
A->method1
A->method2
A->method1
B->method2
A->method1
A->method2

8) Find the output-
    class A { };   
    class B : A { };   
    class C
    {
        public virtual void Collide(A a)
        {
            Console.WriteLine("C -> A");
        }
        public virtual void Collide(B b)
        {
            Console.WriteLine("C -> B");
        }
    }
    class D : C
    {
        public override void Collide(A a)
        {
            Console.WriteLine("D -> A");
        }
        public override void Collide(B b)
        {
            Console.WriteLine("D -> B");
        }
    }
static void Main(string[] args)
        {
            C c = new C();
            A a = new A();
           
            B b = new B();

            c.Collide(a);
            c.Collide(b);

            D d = new D();
            d.Collide(a);
            d.Collide(b);

            C cd = new D();
            cd.Collide(a);
            cd.Collide(b);

            A ab = new B();
            c.Collide(ab);
            cd.Collide(ab);
        }
Ans -
C -> A
C -> B
 D -> A
 D -> B
 D -> A
 D -> B
 C -> A
 D -> A

No comments:

Post a Comment