-1

I'm trying to study the difference between deep copy and shallow copy in Java. I created an example where I use a generic class OrganizedGroup to manage Enterprizes,Music groups and others. Then, I create a container with different groups of type OrganizedGroup.

 import java.util.ArrayList;

interface Leader {

  public void command();
}

class Singer implements Leader {

  @Override
  public void command() {
    System.out.println("I'm a singer");
  }
}

class Boss implements Leader {

  @Override
  public void command() {
    System.out.println("I'm a boss");
  }
}

class OrganizedGroup<T extends Leader> {

  private T mandador;

  public OrganizedGroup(T m) {
    mandador = m;
  }

  public void posturaOficial() {
    mandador.command();
  }

  public void setLeader(T m){
    mandador = m;
  }
}

class MusicGroup extends OrganizedGroup<Singer> {

  public MusicGroup(Singer c) {
    super(c);
  }

  public void playMusica() {
    System.out.println("Playing music....");
  }
}


class Enterprize extends OrganizedGroup<Boss> {

  public Enterprize(Boss j) {
    super(j);
  }

  public void makeDeal() {
    System.out.println("Making a deal....");
  }
}


public class Pruebas {

  public static void main(String[] args) {
    ArrayList<OrganizedGroup<? extends Leader>> go = new ArrayList();
    OrganizedGroup<? extends Leader> g = new MusicGroup(new Singer());
    go.add(g);
    g = new Enterprize(new Boss());
    go.add(g);

    for (OrganizedGroup j : go) {
      j.posturaOficial();
    }

    OrganizedGroup< ? extends Leader> t = go.get(0);
    t.setLeader(new Singer()); //compile error
  }
}

On the last line I try to modify the leader of the first group ( I need to do that in order to see the difference between shallow copy and deep copy (say I cloned go into go2)) however it gives the following compile error:

Singer cannot be converted to CAP#1 where CAP#1 is a fresh type-variable: CAP#1 extends Leader from capture of ? extends Leader

I need answer to solve this problem, not an explanation of the compile error but more importantly of what is conceptually wrong, what should be change and why.

Please,don't focus on bad design matters I actually made a different implementation of this (OrganizedGroup being abstract) but I need to solve this problem to practise with generics.

user1868607
  • 2,558
  • 1
  • 17
  • 38
  • where is the clone() Method? – Bilbo Baggins Sep 03 '15 at 11:22
  • [Implements vs. Extends. When to use? What's the Difference?](http://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference) – Jordi Castilla Sep 03 '15 at 11:23
  • @BilboBaggins i made the example to study the difference between deep and shallow copies but to do it I first need to solve last line error that's why I didn't write a clone method yet – user1868607 Sep 03 '15 at 11:27
  • I would suggest you to read the difference first. Basically in shalow cloning you only copy references of object within the object that you are trying to clone and not the object it self, while in deep cloning you are cloning everything i.e. if B object is inside A then shallow cloning on A will create another instance of A but will still be referencing the same B object, while in deep cloning you will get another object of B. – Bilbo Baggins Sep 03 '15 at 11:52
  • Popular way to perform deep cloning is serialization. – Bilbo Baggins Sep 03 '15 at 11:52
  • What is go2 ? I cannot spot it in your source code. – Matthias J. Sax Sep 03 '15 at 12:44

1 Answers1

0

There is no need to write:

OrganizedGroup< ? extends Leader> t = go.get(0);

Instead I wrote just:

OrganizedGroup t = go.get(0)

and the same can be done in all main's sentences. No compile error.

user1868607
  • 2,558
  • 1
  • 17
  • 38