Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one interface and a class see below:
C#
interface ILastName
{
   string LastName();
}

class MyName : ILastName
{
   public string FirstName()
   {
      return "Michael";
   }

   public string LastName()
   {
      return "Dutt";
   }
}


Now see my following questions and assumptions
1)
C#
MyName myNameObj=new MyName();


in the above line of code new Myname() helps to create Object of class and it allocate memory in the heap that means all the method of MyName is present in heap for access. and myNameObj contain reference to that memory so means we can access all the method by using myNameObj. (name of method).

But
2)
C#
ILastName obj=new MyName();


In the above line same thing happen new Myname() creating object of class n taking memory in heap that means all method available to access in heap.

But we are able to access only those methods which are present in Interface see following line I can access
C#
obj.LastName();


but following is not possible why??
C#
obj.FirstName();


why ?? obj is also holding the reference to the memory which is taken by MyName class in heap same as myNameObj, then we are not able to access method which is not present in interface??? please help me out for my this confusion.
Posted
Updated 19-Jun-14 22:50pm
v2

Simple: at compile time, the system only knows that obj holds a class instance that implements ILastName:
C#
ILastName obj=new MyName();
Because you have explicitly declared it as being of the type ILastName

That you have assigned a MyName instance to it doesn't matter - the system can't be sure that at some point you won't assign a MyOnlyNameIsMyLastName instance, or a ArtistFormerllyKnownAs instance, even.

If you declare a variable as TypeX then you can only use properties, fields, and methods that are declared in TypeX or a class / Interface it acquires through inheritance - you cannot use anything that is declared in a derived class, because not all derived classes are required to implement it.
 
Share this answer
 
Because FirstName() is not part of the interface.
If you want to access it, you have to make a cast to the concrete class:
C#
ILastName obj = new MyName();
Console.Write(obj.LastName());
Console.Write(((MyName)obj).FirstName());
 
Share this answer
 
ILastName obj=new MyName();
the obj is ILastName, the compiler doesn't know whether it is MyName or not.

In a way, ILastName is bigger than MyName, or we can say it's some sort of a container of MyName.
Like someone is from New York, and he is introduced like "He is from USA", nobody will say that he is a New Yorker. But if he was introduced as "from New York", we can also say that he is an American.

In the example above, ILastName=USA, MyName=NewYork, the "someone"=obj.
Eng is not my language, I tried.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900