Reputation: 17
As I learn that interface is also used for encapsulate method but in the following code by casting ojb
to MainClass
I am able to access other method from Mainclass
which I did not declare in interface so now where is encapsulation occur.
class Program
{
static void Main(string[] args)
{
IInterface obj = new MainClass();
Console.WriteLine(((MainClass)obj).FullName()+" " +obj.LastName());
Console.WriteLine(((MainClass)obj).SayHello());
Console.ReadKey();
}
}
public interface IInterface
{
string LastName();
}
public class MainClass:IInterface
{
public string FullName()
{
return "Raman Singh";
}
public string SayHello()
{
return "Hello Sir111";
}
public string LastName()
{
return "Chauhan";
}
}
Upvotes: 1
Views: 101
Reputation: 2178
First thing you mentioned that "interface is also used for encapsulate" which is wrong. Interfaces are used for Abstraction.
And in your code you have created object of your class and also casting it when you are calling the methods. Now if your methods are public then by casting it to your class you can obviously access it.
You can hide other methods by abstraction only when your client doesnt aware about concrete implementation of your class, and must access your methods via interface. Then methods which are declared in interface can be accessible to the client.
Trying to answer your comment in simple language. By the statement
IInterface obj = new MainClass();
you are creating an object of type MainClass. Which will create new instance and returns its reference to variable of type IInterface, but at runtime.
Now when you are assigning reference to interface type, though the object is of MainClass, you are accessing it using interface variable. But interface has no knowledge of other methods in your class which are not inherited from that interface. So no need to talk about object residing in heap at run time, because even compiler would not allow you to access the other methods of your class which interface variable doesn't knows.
You were able to call other methods only because you were casting the interface variable again to your Class type. Hope it makes things easy to understand.
Upvotes: 1
Reputation: 387
Your code is the same coding to an instance field instead of an Interface as the cast (MainClass)obj is changing it back to it.
Upvotes: 0
Reputation: 3024
You are confused among interface
and encapsulation
. This is not what interfaces are for.
An interface contains definitions for a group of related functionalities that a class or a struct can implement. By using interfaces, you can, for example, include behavior from multiple sources in a class.
whereas
Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing.
You are actually holding the MainClass
's instance in your obj
, once you cast, as you said. because you actually initialized it using `new MainClass();'. This will call MainClass's constructor to initialize obj. Thats why you can access other methods.
Upvotes: 4
Reputation: 381
When you cast your object to MainClass, the object reference changes from IInterface to MainClass. Therefore, all of MainClasses Mehtods are available since the visibility of said methods depends on the object reference. If you dont cast your object to MainClass and let its reference be of type IInterface, only the LastName method is visible.
Upvotes: 0