Created
August 22, 2016 08:24
-
-
Save danishsatkut/854dc643fa66808a63a8df91a5317b13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
interface IArray { | |
public void add(Object element); | |
public void addAll(Object elements[]); | |
} | |
class Array implements IArray { | |
private ArrayList<Object> a = new ArrayList<Object>(); | |
public void add(Object element) { | |
a.add(element); | |
} | |
public void addAll(Object elements[]) { | |
for (int i = 0; i < elements.length; ++i) | |
a.add(elements[i]); // this line is going to be changed | |
} | |
} | |
// Don't inherit from Array, instead use composition | |
class ArrayCount implements IArray { | |
private int count = 0; | |
private Array array = new Array(); | |
public void add(Object element) { | |
array.add(element); | |
++count; | |
} | |
public void addAll(Object elements[]) { | |
array.addAll(elements); | |
count += elements.length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment