GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 370 of 436    Print  
In which scenerio we use interfaces,abstract and concrete class?Which one is better and appropriate?Differentiate these three terms in depth.

  
Total Answers and Comments: 3 Last Update: January 19, 2007     Asked by: Amitthakur 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 30, 2006 00:08:26   #1  
arindam.mallik Member Since: October 2006   Contribution: 6    

RE: In which scenerio we use interfaces,abstract and c...
The decision tree for this is pretty detailed. :) In most cases using an abstract class is the right thing to do if you're trying to create a common class from which others will derive and which isn't fully specified. Interfaces are nice if you don't want to force classes to have a single root class in their hierarchy as a single class can implement multiple interfaces. Concrete classes should be used if it's fully specified--i.e. subclasses don't have any hooks into which they must provide functionality.
 
Is this answer useful? Yes | No
December 24, 2006 14:00:33   #2  
kiran        

RE: In which scenerio we use interfaces,abstract and c...

Interface - used to define a skeleton. Classes who want to confirm to that skeleton implementsts the interface

Abstract class - Use abstract class when you want to provide some functionality to the user and at the same time would like to enforce some skeleton (structure) for the users of your class

Concrete class - Use a concrete class to represent an object (data & methods) which can be extended or reused.


 
Is this answer useful? Yes | No
January 19, 2007 11:33:51   #3  
saurabhd Member Since: January 2007   Contribution: 2    

RE: In which scenerio we use interfaces,abstract and c...

Use following guidelines for each of the abstractions:

Interface:
-- If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.
For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.
-- If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .

Abstract Classes
-- When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.
For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.
-- The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape