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

Grails pass object to the view and back again

grails controller

I have some data that I need to persist through multiple actions within my Grails app. Due to the nature of the data, I would prefer not to store the data in the session. Here is an example of wh...

Advantages/disadvantages to user registration and login methods

design login registration user-registration

Take the following three user registration/login methods (or add your own):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.