C#: Inheriting from class while implementing an interface

I have a public interface XFace and XClass.  
I have another class that inherits from XFace and implements XClass called XHybrid.

However, I don't understand why i can only see the methods for the Class object when i write:

XClass x = new XHybrid()  //only XClass methods show...How can I make it where I see all methods from both XFace and Xclass?

Similarly, if I write:

XFace  x  =  new xHybrid()  //only see methods from Interface.
LVL 21
silemoneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Can you please post all the classes and interfaces that are involved in this question so that we may give a correct solution. Thanks
käµfm³d 👽Commented:
Think about it:  What is defined within XFace? Assume for a moment that you could do:

XFace instance = new XFace();
...what would you expect to see? Whatever the type of the variable that you are dealing with dictates what you will see in Intellisense. It doesn't change the fact that the interface variable points to a concrete class--meaning all of its defined members still exist even though you cannot see them. If you wanted to see all of the defined members, then you would have to cast the variable:

e.g.

XFace  x  =  new xHybrid();

((xClass)x).SomeXClassMethod();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

käµfm³d 👽Commented:
To give you a real-world comparison, if told you:  "Over there is a car..." then you wouldn't know anything specific about the car, right? How many doors does it have? What color is it? How big is the engine? If I "cast" the car thus:  "Over there is a red Honda Civic hatchback..." then you now have a bit more information about the car, correct? This is what's going on with your variables.
silemoneAuthor Commented:
käµfm³d 👽  <--- So you're saying its most likely an Intellisense error and I can still use my methods from interface if I use polymorphism to make it a child class of XClass, it will still see its other contracted XFace  methods that since it is also a XFace at runtime?
Fernando SotoRetiredCommented:
which ones are Class, XFace and XHybrid?
silemoneAuthor Commented:
I was trying to make the object more of an hybrid so I could avoid having multiple objects and casting...it could be either/or.  Guess that's not the case...so you are saying i will need multiple objects so in a sense why inherit the second class...just use contract methods of Interface?
silemoneAuthor Commented:
Fernando -

XHybrid - would be the Concrete Class -> it implements both the interface and parent class -
File Name: TextFileHandler

XClass - would be class that XHybrid inherits come implementations/methods from that will work for all files.
File name: FileHandler

XFace - would be the interface which offers standardization of all classes that will handle files...
File name: IFileHandlerInterface

I'm using Strategy Design Pattern to ensure SOLID principles are enforced: base class will never need to be modified, separation of concerns, Interfaces are small and to the point, yada yada yada.

Hope that helps...
käµfm³d 👽Commented:
It's not an error; it's by design. The reason is because you might have multiple classes which implement the XFace interface, but not all of them have the same members. If you were to try and treat all of the classes the same --even with their difference of members--then you would encounter casting issues.

e.g.

interface XFace
{
    void SomeMethod();
}

class ClassA() : XFace
{
    public void SomeMethod()
    {
        Console.WriteLine("SomeMethod from ClassA.");
    }
    
    public void AnotherMethod()
    {
      Console.WriteLine("AnotherMethod from ClassA.");
    }
}

class ClassB() : XFace
{
    public void SomeMethod()
    {
        Console.WriteLine("SomeMethod from ClassB.");
    }
}

Open in new window


XFace instance = new ClassB();
((ClassA)instance).AnotherMethod();   // InvalidCastException

Open in new window

käµfm³d 👽Commented:
Generally speaking, when you start coding against interfaces, you don't care what the concrete implementat is or does. You only work with what the interface defines.
silemoneAuthor Commented:
Understood...I see your point kauf...thx for your insights...that makes sense in your example, but what about when a class inherits and implements?  Why wouldn't the class doing the inheriting and implementing be able to see all of the methods of both?  I'm trying the casting methodology you used in your first post to test.  Maybe that explanation went over my head a bit...
silemoneAuthor Commented:
Well I will do more research, but the casting will cause the methods to show themselves and give me access without me creating a new object.  Awesome!  Thanks to both of you.
käµfm³d 👽Commented:
Why wouldn't the class doing the inheriting and implementing be able to see all of the methods of both?
The class doing the implementing can. But the interface cannot see the inherited members nor can the base class see the interface members. In other words:

interface XFace
{
    void SomeMethod();
}

class ClassA() : XFace
{
    public void SomeMethod()
    {
        Console.WriteLine("SomeMethod from ClassA.");
    }
    
    public void AnotherMethod()
    {
      Console.WriteLine("AnotherMethod from ClassA.");
    }
}

class ClassB() : BaseClass, XFace
{
    public void SomeMethod()
    {
        Console.WriteLine("SomeMethod from ClassB.");
    }
}

class BaseClass()
{
    public void BaseMethod()
    {
      Console.WriteLine("BaseMethod from BaseClass.");
    }
}

Open in new window


ClassB instance = new ClassB();

instance.SomeMethod();          // OK
instance.BaseMethod();          // OK

XFace castedToInterface = instance;  // Not really a cast, but for this explanation think of it as such

castedToInterface.SomeMethod(); // OK
castedToInterface.BaseMethod(); // Error

BaseClass castedToBaseClass = instance;  // Not really a cast, but for this explanation think of it as such

castedToBaseClass.SomeMethod(); // Error
castedToBaseClass.BaseMethod(); // OK

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.