Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0

What are some real world examples of how to use abstract classes and interfaces?

Posted 18-Nov-09 0:07am
Edited 19-Nov-09 0:50am
AspDotNetDev190.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Comments
vicky_geek at 17-Sep-10 16:14pm
   
Reason for my vote of 5 Automatic vote of 5 for accepting answer.
Jayanti123 at 23-Dec-11 2:57am
   
There is no real example in this.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

An abstract class is a class that you cannot create an instance of. It can provide basic functionality, but in order for that functionality to be used, one or more other classes must derive from the abstract class. One of the major benefits of abstract classes is that you can reuse code without having to retype it. That has a plethora of benefits, such as reducing bugs and making coding faster. A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it". Instead, you see a dog or a cat or a pig... all animals. The point is, that you can never see an animal walking around that isn't more specifically something else (duck, pig, etc.). The Animal is the abstract class and Duck/Pig/Cat are all classes that derive from that base class. Animals might provide a function called "Age" that adds 1 year of life to the animals. It might also provide an abstract method called "IsDead" that, when called, will tell you if the animal has died. Since IsDead is abstract, each animal must implement it. So, a Cat might decide it is dead after it reaches 14 years of age, but a Duck might decide it dies after 5 years of age. The abstract class Animal provides the Age function to all classes that derive from it, but each of those classes has to implement IsDead on their own.

Now, an interface is like an abstract class, except it does not contain any logic. Rather, it specifies an interface. So, there might be an interface called IFly. This might have the methods GoForward and GoDown. Those methods would not actually contain any logic... each class that implements interface IFly would have to implement those GoForward and GoDown methods. You could have classes Duck and Finch implement interface IFly. Then, if you want to keep a list of instances that can fly, you just create a list that contains items of type IFly. That way, you can add Ducks and Finches and any other instance of a class the implements IFly to the list.

So, abstract classes can be used to consolidate and share functionality, while interfaces can be used to specify what the common functionality that will be shared between different instances will be, without actually building that functionality for them. Both can help you make your code smaller, just in different ways. There are other differences between interfaces and abstract classes, but those depend on the programming language, so I won't go into those other differences here.

  Permalink  
v5
Comments
BrianBissell at 4-Aug-11 9:27am
   
Great explanation Just a little sad with the IsDead() method example lol
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Do you mean real-world as in "A live software system which includes  Abstract classes or interfaces" or do you mean "A contrived example which demonstrates their usefullness"?

If you mean the latter think of Vehicle as an abstract class. You can't yet do anything with it because you have no idea what it does, or how to drive it.

abstract class Vehicle{}

Vehicles could be split into morotized and pedal-powered, but still this is abstract, we still dont know what to do with it.

abstract class MotorVehicle : Vehicle {} 

abstract class PedaledVehicle : Vehicle {}

You could now define a concrete (non-abstract) class, like car.

class MotorCar : MotorVehicle {}

 Intefaces come in handy you can only inherit from one base class. So imagine some vehicles are drivable, others are remote controlled, some vehicles use a stearing wheel, others dont

interface IDrivable{}

interface IHasStearingWheel{}

Now you could derive a DrivableMotorCar from its base clas, and also implement other behaviours.

class DrivableMotorCar : MotorVehicle, IDrivable, IHasStearingWheel {}

  Permalink  
Comments
yogish at 1-Jun-10 0:38am
   
Excellent explanation !! thx
Prosanta Kundu online at 9-Jul-10 0:25am
   
Reason for my vote of 5 Really good.
sameer.gogia at 12-Nov-12 11:11am
   
I just signed in to Rate your answer ***** Thanks a lot :)
Member 10050153 at 17-Jun-13 10:19am
   
what
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I Just find a suitable reference... Pls refer to this...
  Permalink  
Comments
yogish at 1-Jun-10 0:39am
   
nice link!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Real world examples? the .net framework is full of them
TextWriter comes to mind and Stream class
where as IEquatable is good example for interface.
 
The way I see it:
Interface - Add behavior to a class, a contract to perform certain operations on a object
Abstract class - Define a base class for several types, like the stream class - I know I will read the data from a stream (which is an object, not a behavior of a certain object) but I don't realy care where the strema comes from.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Interface and Abstract class
 
Write a program to maintain University marking Database.
Student is Abstract class, it has Roll no., Name, subject_1_mark attributes.
Show_student_data() is abstract method.
Get_student_data() is non abstract method.
ISport is an Interface, having attribute sport_grace_marks=5.
Show_sport_mark() is a method.
IExService_Man is an Interface, having attribute ExService_Man_grace_marks=10.
Show_ExService_Man_mark() is a method.
Result is Class, it is inherited from Student, ISport, IExService_Man.
Total_marks=subject_1_mark + sport_grace_marks + ExService_Man_grace_marks.
Show_result() is method of Result class.
  Permalink  
Comments
mir hassan at 4-May-13 14:13pm
   
its very nice Article

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 500
1 Tadit Dash 330
2 Sergey Alexandrovich Kryukov 250
3 OriginalGriff 163
4 Peter Leow 140
0 Sergey Alexandrovich Kryukov 9,435
1 OriginalGriff 5,498
2 Peter Leow 4,230
3 Maciej Los 3,540
4 Abhinav S 3,353


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid