Inheritance and Types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I had the following classes,

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

Then create an instance class from class C,

[Type] instance = new C();

What are the allowable types for C?
 
A, B, C, ISomeInterface, IAnotherInterface

This compiles fine...

class Class1
{
[STAThread]
static void Main(string[] args)
{
ISomeInterface c1 = new C();
IAnotherInterface c2 = new C();
A c3= new C();
B c4= new C();
C c5= new C();
}
}

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

public interface ISomeInterface
{
}

public interface IAnotherInterface
{
}

public class B
{
}
 
Hi,

Did you tried it ?

It's extremely simple to test, just change it to each one possible value
and you will get your answer

tip:
There will be one more that what you may see at beginning :)


Cheers,
 
Hi,

[Type] can be:

B,
ISomeInterface,
IAnotherInterface,
A
object

or any other classes/interfaces that B implement.

Cheers,
 
What are the implications of using one type over the other?

Dan Bass said:
A, B, C, ISomeInterface, IAnotherInterface

This compiles fine...

class Class1
{
[STAThread]
static void Main(string[] args)
{
ISomeInterface c1 = new C();
IAnotherInterface c2 = new C();
A c3= new C();
B c4= new C();
C c5= new C();
}
}

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

public interface ISomeInterface
{
}

public interface IAnotherInterface
{
}

public class B
{
}

Chris Fink said:
If I had the following classes,

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

Then create an instance class from class C,

[Type] instance = new C();

What are the allowable types for C?
 
This question is a little deeper.

If you need to ask this question, use the syntac "C c1 = new C();"

I'd google on "object oriented design" and "object oriented programming"
Check this out too...
http://www.firststep.com.au/education/solid_ground/oo.html




Chris Fink said:
What are the implications of using one type over the other?

Dan Bass said:
A, B, C, ISomeInterface, IAnotherInterface

This compiles fine...

class Class1
{
[STAThread]
static void Main(string[] args)
{
ISomeInterface c1 = new C();
IAnotherInterface c2 = new C();
A c3= new C();
B c4= new C();
C c5= new C();
}
}

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

public interface ISomeInterface
{
}

public interface IAnotherInterface
{
}

public class B
{
}

Chris Fink said:
If I had the following classes,

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

Then create an instance class from class C,

[Type] instance = new C();

What are the allowable types for C?
 
Hi Chris,
What are the implications of using one type over the other?

abstraction.

If you use one of the inherited types, your code can follow the Liskov
Substitution Principle, which is to say that your code and deal with the
child type (C) in the same way that it could deal with any of it's base
types, allowing you to substitute an object of the child type for an object
of the base type without changing the client code.

This is fundamental to OO programming.
http://blogs.msdn.com/nickmalik/archive/2004/12/21/328727.aspx

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Chris Fink said:
What are the implications of using one type over the other?

Dan Bass said:
A, B, C, ISomeInterface, IAnotherInterface

This compiles fine...

class Class1
{
[STAThread]
static void Main(string[] args)
{
ISomeInterface c1 = new C();
IAnotherInterface c2 = new C();
A c3= new C();
B c4= new C();
C c5= new C();
}
}

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

public interface ISomeInterface
{
}

public interface IAnotherInterface
{
}

public class B
{
}

Chris Fink said:
If I had the following classes,

public class A: B, ISomeInterface
{
}

public class C: A, IAnotherInterface
{
}

Then create an instance class from class C,

[Type] instance = new C();

What are the allowable types for C?
 
Back
Top