C# .NET - How here only A class method is accessible ?

Asked By anbu n on 02-Jul-13 03:01 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class A
{
public void Amethod()
{
Console.WriteLine("Amethod");
}
}

class B : A
{
public new void Bmethod()
{
Console.WriteLine("Bmethod");
}
}




class Program
{
static void Main()
{

A ref2 = new B();

ref2.Amethod(); // how here only A class method is accessible ? Pls explain

B ref3 = new B();

ref3.Amethod();
ref3.Bmethod();

}
}
}
-------------------
in the above code ,how here only A class method is accessible ? Pls explain
karur krishna madhu replied to anbu n on 26-Jul-13 02:46 AM
your requirement is class a and you have been given an object of class b during runtime. your requirement should be ful filled hence you can access class a methods and also its accessable on condition that B:A. but you cannot access methods of class b in the same case A ref2= new b(). as the strict rule of OOPS says i will give you what is needed not more than that your need is methods of A using ref2 you are able to access those.you cannot access class b methods even though the object is of class b .