Garry Chauhan
Garry Chauhan

Reputation: 17

Interface variable not able to access all class methods

I have one interface and a class see below:

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)

 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)

 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

obj.LastName(); 

But following is not possible why?

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?

Upvotes: 0

Views: 109

Answers (3)

Patrick Hofman
Patrick Hofman

Reputation: 157126

That is because the variable obj is typed as a ILastName.

Hence, all properties, methods, etc. from ILastName are visible. They are there, but you need to cast it to the right type in order to access them.

If you try this, it works:

MyName obj = new MyName();

This would work too:

var obj = new MyName();

Upvotes: 1

Amblll
Amblll

Reputation: 88

Only what is in the interface ILastName is available for the obj(ILastName obj=new MyName();) usage. The interface only know the method(string LastName();) therefore only can access what is in the interface.

MyName myNameObj=new MyName(); is an object from MyName class, therefore it is able to access all the methods and variables in the MyName.

MyName:ILastName shows MyName is a class implement from the ILastName interface therefore it have to implement all methods in the ILastName .

MyName myNameObj=new MyName(); and ILastName obj=new MyName(); creates object therefore the are memory allocated to it.

Hope my explanation aids your understanding.

Upvotes: 0

Dennis_E
Dennis_E

Reputation: 8904

There's a difference between compile-time Type and run-time Type. The run-time Type of obj is MyName, while the compile-time Type is ILastName. What if I did the following?

class MySecondName:ILastName
{
    public string SecondName()
    {
        return "Michael";
    }
    public string LastName()
    {
        return "Dutt";
    }
}

ILastName obj;
int i = new Random().Next() % 2;
if (i == 0) obj = new MyName();
else obj = new MySecondName();

There's no way to tell what the type of obj will be. All that is known is that it's a ILastName, so the only method it is known to have is LastName().

Even if there's only 1 possible Type, as in your example, the compiler will not look at that. The only thing it considers is the compile-time Type.

Upvotes: 0

Related Questions