Click here to Skip to main content
15,883,922 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can u please explain real time example of abstract class and interface . I know the difference between them but Still I am not understand in which situation abstract class is use and in which situation interface is used
Posted
Comments
PIEBALDconsult 25-Nov-15 8:30am    
Not even Microsoft seems to know that.

An abstract class allows you to derive a "concrete" class which inherits a set of core functionality: in much the same way as "a Car" in the real word is an abstract object, but "a Ford Fiesta" allows you to drive to the shops. The "Ford Fiesta" class inherits a set of expectations that are common to all Cars: it will have four wheels, an engine, and a place for a driver to sit and control it's speed and direction. But you can't walk into a dealership and buy "a car" - you have to go there expecting to get a specific make and model of car: "Ford" and "Fiesta" and from there decide on an engine size and type, a trim level, and so forth.

An Interface is different: it doesn't allow you to inherit any existing behaviours but enforces that you must do some things in the right way - it's a contract rather than a base class. In the real world, think of an Interface as the membership of a club: provided you pay your dues on time, and follow the club rules you can remain a member and get the benefits of such membership. But there is nothing stopping you from joining a second club at the same time - provided you can also obey its rules, and pay its dues.
 
Share this answer
 
An Interface is a class signature. It does not contain any functionality and cannot hold values. It is used to ensure that classes implementing the interface have the signature methods and/or properties, which can then be called by a calling function.

An Abstract class can contain functionality but, like an interface cannot be instantiated. You can have static methods that don'r require and instance or you can inherit from it knowing that the core functionality is available.

That's the basic. There is much more detail and many examples on google
 
Share this answer
 
v2
Comments
Ravi Bhavnani 25-Nov-15 8:35am    
> It is used to ensure that classes implementing the interface have the signature methods
and/or properties :)

/ravi
Andy Lanng 26-Nov-15 3:56am    
good point - updated ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900