Reputation: 2509
i have two classes which have some common methods like funcA(), funcB()
and some methods are only related to its class...
what i did is made interface of TestInterface
public interface TestInterface
{
void funcA()
void funcB()
}
public class ClassA : TestInterface
{
public void funcA()
{
Console.WriteLine("This is ClassA FuncA()");
}
public void funcB()
{
Console.WriteLine("This is ClassA FuncB()");
}
public void myFuncA()
{
Console.WriteLine("This is My Own Function A");
}
}
public class ClassB : TestInterface
{
public void funcA()
{
Console.WriteLine("This is ClassB FuncA()");
}
public void funcB()
{
Console.WriteLine("This is ClassB FuncB()");
}
public void myFuncB()
{
Console.WriteLine("This is My Own Function B");
}
}
public static void main()
{
TestInterface test = new ClassA();
test.funcA();
}
as u see in above two classes. i have two functions myFuncA() and myFuncB() are not part of interface. they only belongs to their own class.
how can i call them from the main method. where i am creating object of TestInterface and initializing it with some child class.???
actually i have separate classes in DAL.
they have some common methods. and some methods related to their own class.
i want to implement Factory Pattern on this.
what should i do now?
Upvotes: 0
Views: 580
Reputation: 405
TestInterface test = new ClassA();
test.myFuncA();
(test as ClassB).myFuncB();
Hopefully this was what you were looking for.
Upvotes: 1
Reputation: 12610
Mohsen I'm really confused with your question
if you just want to call MyFuncA() or MyFuncB() just you should create an instance of their own calss like
ClassA a = new ClassA();
a.FuncA(); //interface member
a.FuncB(); // interface member
a.MyFuncA(); //class A Member
I can't undrestand why you are trying to create an instance of your Interface and then call subclass methods!!!! Am I right ? or Imisunderstood your issue ?
Upvotes: 0
Reputation: 2509
is it good to add methods of ClassA and ClassB into interface and provide null implementation in other class?
like
public interface TestInterface
{
void funcA();
void funcB();
void myFuncA();
void myFuncB();
}
public class ClassA:TestInterface
{
public void funcA()
{
// some code here
}
public void funcB()
{
// some code here
}
public void myFuncA()
{
// my own code here
}
public void funcB()
{
// null implementation
}
}
and vise versa of this in ClassB
Upvotes: -1
Reputation: 17904
Try casting. I assume from the question you meant something like this:
public void simpleMethod(TestInterface variableName) {
if (variableName is ClassA) {
((ClassA)variableName).myFuncA();
}
}
Upvotes: 0
Reputation: 2308
You can cast the TestInterface object, but it defeats the purpose of polymorphism...
public static void main()
{
TestInterface test = new ClassA();
test.funcA();
((ClassA) test).myFuncA();
}
Upvotes: 0
Reputation: 41858
Make a new interface for ClassA and ClassB and you just need to cast the class to the correct interface to see the new functions.
public class ClassA : TestInterface, TestInterfaceA {
}
So, when you get the concrete instance just cast that to TestInterfaceA to see the functions specific to this class.
Upvotes: 0
Reputation: 284786
If you're asking whether you can do something like:
public static void Main(string[] a)
{
TestInterface test = new ClassA();
test.myFuncA();
}
the answer is no. You would have to cast it to ClassA first. The fact that you think you need to do this indicates there is probably something wrong with your design.
Upvotes: 2