0

So, I have this lecture and I'm preparing for a test. I have to write code so that my code passes each Junit test. One thing I always dont know how to figure out is, when should I use a interface or subclasses...

Could someone please tell me how can I decide which to use based on the Junit tests?

I have a train, in with there are two types of train: TGV and Pendular

P.S I know the test name explicitly says subclass, but ignore that...

public class RailRoadTests {

@Test
public void testTrain() {
    Train c1 = new Train("Regional");
    assertEquals("Regional", c1.getName()); 

    Train c2 = new Train("Intercidades");
    assertEquals("Intercidades", c2.getName()); 
}

@Test
public void testSubclassesTrain() {     
    Train tgv = new TGV("Speeder");
    assertEquals("Speeder", tgv.getNome());

    Train pendular = new Pendular("Alfa Pendular");
    assertEquals("Alfa Pendular", pendular.getNome());
}
B.Crz
  • 59
  • 8
  • 2
    Using an interface or a subclass are two entirely different things, and have entirely different purposes. I don't understand what you are trying to ask. – Mark Rotteveel Mar 11 '16 at 13:40
  • 1
    Either one would work here. – John Kugelman Mar 11 '16 at 13:41
  • I also do not understand the question. The point is: you do need the subclass anyway; as you have to create some object using "new". Now; if you assign that created object like `InterfaceName myvar = new(...` or if you do `SubclassName myvar = new...` is just a subtle detail. And - the decision which of the two versions is "better" can't be answered in general. Such questions really only make sense in the context of a "real" problem with real requirements. – GhostCat Mar 11 '16 at 13:45
  • You can use this thumb rule: If you need common behaviour between all of your subclasses uses a base abstract class. On the other hand if there isn't default behaviour, us an interface. However if you're using Java 8, you can use an interface with default method implementation so you can choose both. – jfcorugedo Mar 11 '16 at 13:48

3 Answers3

0

A subclass is best if both types of trains share similar functionality or code. An interface allows the same method signatures, but don't contain actual code (unless you're using java 8).

FransGuelinckx
  • 147
  • 2
  • 7
0

An interface ensures that each class that implements it has the same method signatures (naming and parameters). Each class may have a completely different implementation of code for those method signatures. They do not necessarily share any code at all, although they could, in theory.

When you subclass, you can have shared code from the base class. And you can also build on base functionality by calling super([parameters]) from within a method. Or you can use existing functionality from base methods that are within the scope of the subclass (protected, public, etc).

Possible duplicate of When to implement an interface and when to extend a superclass?

Community
  • 1
  • 1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
0

If the subclass wants to extend two or more classes(Multiple Inheritance) then use Interface and if you are extending only one class in your subclass then use Inheritance(Subclass) feature.

Ketan G
  • 507
  • 1
  • 5
  • 21