When should you use Abstract Class vs Interface while
programming? Give 1 Example
Answers were Sorted based on User's Feedback
Answer / rupali
Abstract class can contain non abstract method.
When we want that all subclass must implement all methods of
base class ;In such situation use interface.In other hand
when we want only some method of base class in our subclass
then use base class as abstract class because abstract class
can contain non-abstract method.
| Is This Answer Correct ? | 45 Yes | 14 No |
Answer / pravin shrirao
Interfaces are used when we want classes should follow the
rules strictly. Rules like naming coventions e.g. if in
customer class one developer add method AddCustomer other
developer while implementing Invoice class could write
method name InvoiceAdd and also could change the Signiture
rules. for addition of customer we have at client code as
Cutomer.AddCustomer(A,B,C); and Invoice.InvoiceAdd(A,B)
To maintain uniformality if we declare interface and define
method Add(A,B) in that interface and implement that
interface in both the classes then the code will be uniform
all over the classes where ever we have implemented
interface and also all concrete classes must contain
methods which are defined in the interfaces.
| Is This Answer Correct ? | 22 Yes | 5 No |
Answer / ashis
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.
Use an abstract class
* When creating a class library which will be widely
distributed or reused—especially to clients, use an abstract
class in preference to an interface; because, it simplifies
versioning. This is the practice used by the Microsoft team
which developed the Base Class Library. ( COM was designed
around interfaces.)
* Use an abstract class to define a common base class
for a family of types.
* Use an abstract class to provide default behavior.
* Subclass only a base class in a hierarchy to which the
class logically belongs.
Use an interface
* When creating a standalone project which can be
changed at will, use an interface in preference to an
abstract class; because, it offers more design flexibility.
* Use interfaces to introduce polymorphic behavior
without subclassing and to model multiple
inheritance—allowing a specific type to support numerous
behaviors.
* Use an interface to design a polymorphic hierarchy for
value types.
* Use an interface when an immutable contract is really
intended.
* A well-designed interface defines a very specific
range of functionality. Split up interfaces that contain
unrelated functionality.
| Is This Answer Correct ? | 5 Yes | 0 No |
What is the mesi? : .NET Architecture
What is the purpose of hard disk? : Dot net architecture
What is abstact class? in which scinario we can use abstact class?
how to change the application name?
Explain difference between machine config vs. Web config? : .NET Architecture
What is the numeric attribute of the textbox control in .net mobile? : Microsoft dot net mobile
Name some changes done since version 3.0?
how to find the 2nd higgest salary in the column for example take table name is employee? And also find bottom 2nd lowest salary ?
11 Answers GeoTechnology, Wipro,
How does .net remoting work?
What other than biginteger has been introduced in system.numerics namespace?
I am working with asp.net 2005 and Crystal report 10. I have an image field on my dataset to show on Crystal report.And the image is shows dynamically from database in image field of CR. The image field is showing well on my Crystal report on its first page with all other data but when i click to open second page of CR,it does not show any data on that.I found that this is because of adding the image field on crystal report. Because when i remove the image field from the CR then it will run both the pages of CR. I am using the Page Load event to bind CR data with Database. And i have already check with the Init event to bind CR , but it still not shows the data on second page when i use image field on that. Please give solution for that.....
what is a namespace?how they are classified?write a program which contains class,object,inheritance?