Act depending on class instance

I have a ClassB inherited from ClassA. ClassA has a nested ClassC with a methodA() in it. In this methodA() I need to do things depending on condition if the outer class ClassA or ClassB. What is the best way to construct if/else statement? Thank you.

Answers


I would better add some virtual methodB in ClassA and override it in ClassB, and call it in methodA of classC.


This is not how OOP works - what if someones extends ClassB to ClassD and you want to further change behavior?

Anyway, instanceof keyword may help you while you refactor your code.

Remember that one of the purposes of inheritance / interfaces and so is to prevent big chains of if/else statements - hiding them into methods won't help getting your code smoother.


Need Your Help

mouseEntered and mouseExited method questions

java swing interface mouselistener system.out

Working on a homework assignment and I'm having issues with figuring out how to print a line of text when a mouse cursor enters and exits a certain colored area (a JPanel called panel in this case)...

WCF - 'The service certificate is not provided for target' error for WCF client calling a WCF service

c# .net wcf

I'm trying to create a test service/client in WCF using Message security, with certificates. I'm using the basic service that Visual Studio creates out of the box, and am calling it from another pr...