1) What is the out put of the following program?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A objA = new A();
objA.method(); // print A
B objB = new B();
objB.method(); // print B
C objC = new C();
objC.method(); // print C
A objAB = new B();
objAB.method(); // print B
B objBC = new C();
objBC.method(); // print B
A objAC = new C();
objAC.method(); // print B
Console.ReadLine();
}
}
class A
{
public virtual void method()
{
Console.WriteLine("print A");
}
}
class B : A
{
public override void method()
{
Console.WriteLine("print B");
}
}
class C : B
{
public new void method()
{
Console.WriteLine("Print C ");
}
}
}
2) Polymorphism using foreach loop?
2) Polymorphism using foreach loop?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class LivingThings
{
public virtual void Move()
{
Console.WriteLine("Spefify the
Item");
}
}
public class Person:LivingThings
{
public override void Move()
{
Console.WriteLine("Walk");
}
}
public class Bird:LivingThings
{
public override void Move()
{
Console.WriteLine("Fly");
}
}
class Program
{
static void Main(string[] args)
{
List<LivingThings> livthing = new List<LivingThings>();
livthing.Add(new Person());
livthing.Add(new Bird());
foreach(LivingThings lt in livthing)
{
lt.Move();
}
Console.ReadKey();
}
}
}
No comments:
Post a Comment