1

I have a base class and an interface. Now I am creating a subclass from these. If I create a reference variable of the interface type to point to an object of the child class, can I access the base class methods using it?

Class BaseClass
{
public void baseClassMethod()
{
.....
}
}  
Interface MyInterface
{
public void Interfacemethod();
}


Class ChildClass:BaseClass, MyInterface
{
....

}


....


main()
{
MyInterface myclass= new ChildClass ();
myclass.baseClassMethod();//Is this possible? y?
 }
1
  • 1
    Why don't you try this? Commented May 20, 2013 at 6:42

1 Answer 1

6

The variable myclass is statically typed as MyInterface, which does not have a method called baseClassMethod() - so no, that won't work. You would need to cast the reference back to BaseClasss or Child (either would be fine), or add the method to MyInterface (or some additional interface).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.