2

I am writing some java code and I want to write methods in my main class Array. This class implements ImplementableClass. The former extends Iterable. The Array class has an type.

ImplementableClass.java:

public interface ImplementableClass<E> extends Iterable<E>{

    public void insertObject(E obj);

    public E removeObj(E obj);

}

Main.java:

public class Array<Integer> implements ImplementableClass<E>{
    public void insertObject(E obj){

    }

    public E removeLastObject(E obj){

    }

    //... main method and others below...
}

I have some questions regarding the code in the two files above.

Reading the java documentation, Iterable is of type E (generic value). From what I understand, interfaces are just "blueprints" of the methods that MUST be used in the class that "implements" them. From a basic point of view, there shall not be any variables in here. With that being said, as you may see I am indeed declaring the methods in my ImplementableClass in Main as well. With that being said, I have a couple of questions:

  1. When declaring my methods from ImplementableClass class in my Array class, this "overrides" the methods from my ImplementableClass class right?

  2. Since "E obj" is the argument in both methods, do they have to be the same whenever I declare my methods in my Array class? What should I pass to the methods? What does "E obj" mean?

  3. I want to create an array that can hold objects of type E. This means that whenever I instantiate a new object-> Array<Integer> theArray = new Array<Integer> I can call the methods I have on my Array class on theArray instance right? (i.e theArray.removeLastObject() ) What should I pass as an argument?

  4. Why would Iterable<E> be of use in this case?

Makoto
  • 96,408
  • 24
  • 164
  • 210
J Tarantino
  • 45
  • 1
  • 4

2 Answers2

4

When declaring my methods from ImplementableClass class in my Array class, this "overrides" the methods from my ImplementableClass class right?

Yes (well, not technically since there's no functionality in an interface to override, but you can use @Override to indicate you're overriding it)

Since "E obj" is the argument in both methods, do they have to be the same whenever I declare my methods in my Array class? What should I pass to the methods? What does "E obj" mean?

They need to be the same as the generic type you've specified when you implement the interface. E obj means that you've declared a parameter called obj that is of generic type E. This means that you're required to define the methods to take that particular type as a parameter.

It would make more sense though, to define the generic type of your interface in the declaration, such as:

public class ArrayClass implements ImplementableClass<Integer>

so you can have methods like:

public void insertObject(Integer obj) {}

public Integer removeObj(Integer obj) {}

Or else you can make your Array class generic, and leave the specification of the generic type to the caller:

public class ArrayClass<E> implements ImplementableClass<E>

I want to create an array that can hold objects of type E. This means that whenever I instantiate a new object-> Array theArray = new Array I can call the methods I have on my Array class on theArray instance right? (i.e theArray.removeLastObject() ) What should I pass as an argument?

In order to do that, you would need to make your Array class generic, like shown above. The argument you pass in would be the same type you specify when you create the array (Integer in your example).

Why would Iterable be of use in this case?

Iterable is of use so you can make use of the iterator features of an array, and the enhanced foreach syntax (for (Object o : someObjectArray) {...})

Also, I would suggest not naming your Array class Array... and perhaps look at making use of already existing Iterable classes to construct what you're doing, but this looks like a learning exercise, so have at it.

HTH

Ryan J
  • 7,910
  • 3
  • 22
  • 28
  • Thank you for such a detailed response. When I follow your advice of making my class generic "public class Array implements ImplementableClass" Eclipse complains saying: "E cannot be resolved to a type". Do you know why this happens? Thanks! – J Tarantino Feb 18 '15 at 08:04
  • @JTarantino You probably have a dangling type parameter somewhere that is never named to a type at compile time. It most commonly occurs when you declare generic types that are unable to be named by a caller creating the object, usually due to clashing types or typos. – Ryan J Feb 18 '15 at 17:22
  • Thank you! Another dumb question... What is exactly the generic type? Is it a Java defined generic type or is it that I just randomly chose as a generic type? Thanks again for your help! – J Tarantino Feb 18 '15 at 23:21
  • @JTarantino `` is a type parameter, and is used to identify a type that is not yet known, but will be once a reference to the class has been created. The `E` is an arbitrary choice for the symbol that identifies the generic class. Generally, you'll most often see `T` or `E` used for these parameters, but it's just a convention most people use. – Ryan J Feb 18 '15 at 23:25
  • Thank you for your help! You solved all my questions regarding this matter. Perhaps you could help me out with this one? http://stackoverflow.com/questions/28597396/default-constructor-issue-when-creating-an-array-java – J Tarantino Feb 19 '15 at 01:49
0
  1. What's actually happening is that you're implementing the interface, not overriding it. Since interfaces (in Java <= 7) don't have an implementation, there's nothing for you to really override. You can use the @Override annotation to indicate that you're implementing a method from an interface.

  2. You whiffed on the generics in your second class. If you really want it to be generic (that is, it can be bound to any object), then you want this declaration:

    public class Array<E> implements ImplementableClass<E>
    

    That <E> is called a type parameter, and it's applied at the class level, meaning any non-static method or field in the class may make use of it.

    E obj is stating that you are willing to accept whatever type of object comes in as an argument. If you declared Array<Integer> intArray = new Array<>();, then E obj would translate internally to Integer obj instead. There's a decent amount of complex operations related to generics; reading up on it would be best.

  3. Be specific as to what kind of data structure you want to use. Arrays and generics do not mix well. If you're creating a generic object array (as in, E[] backingStore), then creating a generic array would be a consideration to take into account.

    Honestly, I'd recommend you use a List instead.

  4. Iterable means that the object you have can be iterated with an enhanced-for statement. Why you'd want to do this is subject to your discretion, but that's why you'd want to use that particular interface.

Community
  • 1
  • 1
Makoto
  • 96,408
  • 24
  • 164
  • 210