?

Log in

Previous Entry | Next Entry

What we have?
Let’s say you have a class called Customer.
Customer class have following functions
Insert, Update and Delete.

What we will do with it?
Now let say there are some user interfaces called as EmployeeUI and ManagerUI which internally uses the Customer class and its functions.


What’s next?
Customer will demand a new UI from us called DirectorUI. Now this UI will also use Customer class and its functions.
But there is a problem. Update function is going to behave differently for this UI.

What we will do now?
We will go and some if conditions in Update function and make it working for all previous UIs as well as this new UI?

Is it the correct way?
Making changes in exiting customer class violates a Object oriented principle called OCP (Open closed principle) which says, “Software module should be closed for modification but open for extension”

Why it is required to follow such principle?
Customer class is already used in 2 UIs and successfully tested and proved.
Now making changes into it may lead to unexpected errors and complete re testing.

What is the correct solution?

Correct solution will be,

  • Make the existing Update function virtual.

  • Create a new class called CustomerForDirector which will inherit from Customer Class.

  • Override the virtual function in new class.

  • Now in DirectorUI just use this new Derived class instead of Customer class.

Also see the below c# OOP interview question video , what is the need of private constructor ?.



Also read http://questpond.blog.com/2013/09/09/can-you-talk-about-abstraction-and-encapsulation-in-detail-net-c-and-oop-interview-questions-with-answers/

http://dotnetinterviewquestion.wordpress.com/2013/09/09/can-you-explain-class-and-object-in-real-time-scenario-c-and-net-oop-interview-questions-with-answers/




Copy right www.questpond.com

Comments

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

March 2017
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Powered by LiveJournal.com