Solved

Concrete vs Abstract vs Interface simple example looking for that makes sense.  No links please

Posted on 2013-01-24
8
432 Views
Last Modified: 2013-01-28
Hello all,

Can someone provide in very simple terms best with an example the difference between the three of these?  I use them throughout my coding but I get confused sometimes where and when to use etc.   Let me know how far off I am here:

using System;

namespace SportsStore.WebUI.Models
{
    public class PagingInfo
    {
        public int TotalItems { get; set; }
        public int ItemsPerPage { get; set; }
        public int CurrentPage { get; set; }
        public int TotalPages 
        {
            get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
        }
    }
}

Open in new window


This seems to be a 'concrete' class.  I can create an instance of this concrete class and assign the variables such as:
PagingInfo pagingInfo = new PagingInfo();

An abstract class is a little more confusing to me.   Is this simply a base class where I can inherit it in a class as a subclass?   For example, maybe I have a user class that contains username, building, group etc. I can inherit this as a subclass in my class to gather the values?

Interface I have always struggled with but this is what I should use where I want to use all the methods and properties.  This also seems to be the best thing to use when working with dependency injection and moq unit testing etc.  Still trying to get a full grip on this for a long time now.   Is an interface really just like an abstract class where you can implement the methods but don't really need to see what they are doing?

Thanks all hope this helps me a little more.
0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38814673
A class (you call concrete) is one you can create an instance of.  It can also be used as the base of another class.

An abstract class is one you can NOT make an instance of.  It is used for common code that is shared between the derived classes.

An interface is a specification of methods exposed.

A class can inherit from ONE class but MANY interfaces.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 150 total points
ID: 38814708
lets take the shapes example, you have class Triangle, class Square and so on.

now, when you think about implementation, you understand that each of the shapes classes should have list of points, right?
the triangle will have list of 3 Point instances and Triangle will have 4.

so instead of declaring  List<Point> as a member in each class shape, you gonna have abstract class which has it as protected member, which allows the concrete classes access to it.

so here one example of using abstract.
the 2nd example will be shared implementation, for instance, if you want to save the shape information to a file.
you do not need to have a separate implementation in each shape, instead you can use a SaveShape function in the abstract class which does that.

the function creates a file and write the coordinates of the shape.

so basically we have something like:
inerface IShape{
void ClaculateArea()
void SaveShape()
}

abstract class BaseShape:IShape{
protected string _shapeName;
protected List<Point> _points;
public BaseShape(shapeName, IEnumerable<Point> points){
_shapeName=shapeName;
_points=new List<Point>(points);
}

public SaveShape(string file){
var lines = _points.Select(point=>String.Format("X-[{0}], Y-[{1}]", point.X, point.Y)).ToArray();
var output = string.Join(lines, Environment.NewLine);
File.WriteAllText(file, output);
}
}

//the ctor of each shape calls the abstract base class ctor with the shape name and coordinates.
class CSquare{
public CSquare(IEnumerable<Point> points):base("Square", points){
}
}
class Triangle{
public CSquare(IEnumerable<Point> points):base("Triangle", points){
}
}

Open in new window


to allow consumers use concrete objects without expose the class it self, we can create ShapeCreator which accept enum shape and return new shape instance as IShape.

public static class ShapeCreator{
public enum ShapeType{Square,Triangle};
public static IShape Create(ShapeType shapeType, IEnumerable<Point> points){
switch(shapeType){
case ShapeType.Square:return new Square(points);break;
case ShapeType.Trianglenew Triangle(points);break;
}
}
}

Open in new window


so to create a shape you use the following:

IShape square = ShapeCreator.Create(ShapeCreator.ShapeType.Square, new Point[]{
new Point{X=1,Y=1},
new Point{X=1,Y=5},
new Point{X=5,Y=1},
new Point{X=5,Y=5},
});

double area = square.CalculateArea();
square.SaveShape("C:\square.log");

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 38814772
sedg where is CSquare being implemented and doesn't the interface IShape both methods need to be used?   In BaseShape you inherit IShape, you implement the SaveShape method btu I don't see CalculateArea being implemented?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 42

Expert Comment

by:sedgwick
ID: 38814821
CalculateArea is implemented differently for rach shape so theBaseShape declares it as abstract which forces the shapes implement it in their classes.
Public abstract class BaseShape:IShape{

Public abstract double CalculateArea();
}

Public class Square: BaseShape{
public virtual double CalculateArea(){
//calculate square based on _points Member
}
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 38815718
0
 

Author Closing Comment

by:sbornstein2
ID: 38825322
these really helped thanks guys.  Kaufmed thanks that link also clarified some things for me.  I just need to get used to when to use certain things.  It seems for dependency injection and moq unit testing interfaces is the best way to go when I can use them.   Thanks
0
 

Author Comment

by:sbornstein2
ID: 38825337
Kaufmed I am reading your other links as well.  Seriously a huge help to me thank you.   That interface response was something I have been looking for a long time now.  It explains it in simple but terms that make a lot more sense now.   Thanks for these links really really helpful.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38829384
No problem. Glad they were useful  = )
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question