-5

As the ttitle said, im asking if is this a good programmation/design way.

I got a class, that can be just an Interface (only got 1 abstract method and few attributes)

As a example of my case, this is similar:

We got a main class Car than could be a truck, auto, moto, ... and has an abstract method void move()

Could I design CAR as interface, and the other concrete classes as a generalization of CAR class? or is this wrong?

public interface Car{
  private int length;
  private float speed;

  public void move();
}

public class truck : Car{
  //Constructor
  public Car(int size)
  {
     length=size;
  }

  public void move()
  {
     //Move code
  }
}

and then

Car myCar = new truck();
myCar.move();

Would be right?

TaW
  • 53,122
  • 8
  • 69
  • 111
  • I'm not entirely sure what you're asking. Is it _"When to introduce an abstract class in the inheritance chain"_? – CodeCaster Aug 14 '15 at 10:08
  • 2
    I'm voting to close this question as off-topic because it is more suited for the guys over at http://codereview.stackexchange.com/ – KeyNone Aug 14 '15 at 10:09
  • 1
    @BastiM the guys over ar CR will be annoyed at this! – DavidG Aug 14 '15 at 10:11
  • @BastiM For [further reading](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users), specifically this: *Please do not vote to close with a custom reason that "it belongs on Code Review"* – DavidG Aug 14 '15 at 10:14
  • @DavidG imo this question isn't about a concrete programming problem or some code not working or ... or ..., but just a design question that will attract opinionated answer either way. I'm not a member of CR, but my initial thought was it would fit in there. Thanks for the meta link - I wasn't aware of that and will change my close vote. – KeyNone Aug 14 '15 at 10:17
  • @BastiM I agree with you completely and until I read that post I'd have done exactly the same as you :) – DavidG Aug 14 '15 at 10:18
  • 1st Im not asking for a Code Review, it was just an example, the easiest i could think, as my current project is "a bit longer" so copying/pasting the whole code (that i still need to debug) is unnecesary and would take so long for everyone to read. – Dante's Dream Aug 14 '15 at 10:50
  • 2nd im just asking if it's right or not, to create an interface (CAR in my example) and then create some other concrete classes (derived from CAR) with a generalization association. – Dante's Dream Aug 14 '15 at 10:52
  • The code isn't broken, you just want someone to tell you if there is a better way. That IS code review. – takendarkk Aug 14 '15 at 17:25

2 Answers2

1

You're mixing up the terms "abstract" and "interface" here.

It is perfectly fine to refer to an instance of a class by the interface it implements. Here you see an interface, ICookieFactory which bakes abstract Cookies:

public interface ICookieFactory 
{
    Cookie BakeCookie(); 
}

public class ChocolateChipCookieFactory : ICookieFactory
{
    public Cookie BakeCookie()
    {
        return new ChocolateChipCookie();
    }
}

public abstract class Cookie
{
    public abstract IEnumerable<Crumb> Crumble();
}

public class ChocolateChipCookie : Cookie
{
    public override IEnumerable<Crumb> Crumble()
    {
        ...
    }
}

ICookieFactory factory = new ChocolateChipCookieFactory();
Cookie cookie = factory.BakeCookie();
foreach (Crumb crumb in cookie.Crumble())
{
    ...
}

An interface tells implementations of it which methods or properties it must support, but cannot provide any implementation code itself. You can't define fields in an interface.

An abstract class can include any number of fields and methods, and abstract methods that must be overridden by child classes.

A single class can implement multiple interfaces but only inherit from a single abstract class.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Thanks for your help. My question was more about "is it right to create generalization subclasses from an interface?", as in my project i had a class that i could turn into an interface. But now and thanks to your answer i realize it'll be much better to use an abstract class (to implement another concrete method) with an abstract method, that would be overriden in child classes. – Dante's Dream Aug 14 '15 at 11:31
0

As far as i Know, yes, its possible and right to create an interface which child classes would be a case of generalization association.

But in my case, and thanks to the only answer of C.Evenhuis, i realized it'll be better to make an abstract class (so i can combine some abstract methods that child classes must override, with some concrete methods that childs can override or simply use).