Interface in Property

S

shapper

Hello,

I have a class Parent. What is the difference if a property is an
interface or a class? For example:

public class Parent {
public PA Pa { get; set; }
public IPA Ipa { get; set; }
}

Where PA is:

public class PA {
public String Name { get; set; }
}

And IPA is:
public interface IPA {
String Name { get; set; }
}

Thank You,
Miguel
 
S

sloan

Let's throw some animals in there to show the concept a little clearer:

public class Parent {
public Cat MyCat { get; set; }
public IAnimal MyPet { get; set; }
}


public class Dog : IAnimal {
public class Dog { this.Name = "Fido";}
public String Name { get; set; }
}


public class Cat : IAnimal {
public class Cat{ this.Name = "Fluffy";}
public String Name { get; set; }
}



Parent p = new Parent ();
p.MyCat = new Cat();
p.MyPet = new Dog();
//p.MyCat = new Dog();//won't work

string name1 = p.MyPet.Name;

bool hatesDogs = true;
if(hatesDogs)
{
p.MyPet = new Cat();
}


string name2 = p.MyPet.Name;



The different is that
1. You cannot instantiate an interface
IAnimal a = new IAnimal(); does not work

2. You can swap in a Cat or a Dog for Parent.MyPet.
 
M

Mr. Arnold

shapper said:
Hello,

I have a class Parent. What is the difference if a property is an
interface or a class? For example:

public class Parent {
public PA Pa { get; set; }
public IPA Ipa { get; set; }
}

Where PA is:

public class PA {
public String Name { get; set; }
}

And IPA is:
public interface IPA {
String Name { get; set; }
}

Thank You,
Miguel


A class implements the interface. The class holds the data in a public
property. An interface is nothing without an instantiated class\object
that implements the interface that addresses the class's public property.

The interface uses the public Name property to set/get the primitive
data from the class's\object's public property Name.

Now another class also implements the Interface, and this class has an
instance of the other class\object passed to it.

Now the two classes can pass data for Name between them via the interface.

The interface is the middle man that allows the two classes to pass data
to each other via the one class's public property that's addressed on
the interface.


The interface can call a method in the called class that returns data to
the public property of the other via the interface.

An interface is not an object, and it can't be instantiated . The
class\object is concrete and can be instantiated. The interface is only
the interface/contract between the two concrete classes/objects
 
A

Arne Vajhøj

I have a class Parent. What is the difference if a property is an
interface or a class? For example:

public class Parent {
public PA Pa { get; set; }
public IPA Ipa { get; set; }
}

I would say that it is the same as for fields and methods
arguments/return value.

Arne
 
P

Peter Duniho

shapper said:
Hello,

I have a class Parent. What is the difference if a property is an
interface or a class?

The only difference is the difference in type that exists between the
interface and class in _any_ context. The fact that it's the type of a
property as opposed to, say, the type of a variable is irrelevant.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top