Title: Interfaces & Polymorphic Fundamentals
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Interfaces and Polymorphic Fundamentals

1. An interface is typically a list of methods required for any class that chooses to implement an interface. Although Java copied a lot of characteristics from C++, Interfaces is one concept that Java added (i.e. C++ really doesn't have the concept of an interface).
2. Consider the following hypothetical interface:


package life;


public interface Living {
	    public void move();
	    public void eat();
	    public void born();
	    public void die();
}


3. A class can implement an interface like the Living interface above with a statement like:
Tree implements Living
{ ....
}
4. When a class implements an interface, it is required to have all of the methods in the interface (i.e. move, eat, born, die, etc.)
5. Go ahead and create classes like Tree, Snake and Cow that all implement the Living interface.
6. With these classes in place, it is now possible to create an array of Living things. For example, the following code could be possible:
Living[] livingArr = new Living[5];

livingArr[0] = new Tree();
livingArr[1] = new Snake();
// .....
7. Try it out, and go through the livingArr and call the methods for all of the objects which implement the Living interface (i.e. move, eat, born, die).

Java uses Interfaces in Data Structures

We will discuss Data Structures in more depth later in the semester. But for the time being, we will introduce the ArrayList class.

1. The ArrayList is like an array, except that the data structure automatically grows. You don't need to know the maximum size when you create it like you would for an array.
2. Although the ArrayList class has many methods, we will focus on 3 methods: size(), get(int), add(Object).
3. With ArrayLists, you can use something call "generics" to specify what you are storing in the ArrayList. We will go into this in more depth later in the semester. If you don't use the generics notation, you will be specifying an ArrayList which stores the Object class ... we will demo this with objects of type String and Person. Note the need to cast on the call to get.
4. Now use Generics to restrict what kind of object can be stored in the ArrayList.
5. Look up ArrayList in the Java java.util package documentation. Note that it implements an Interface called List. Redo your example to create a data type of List, but have the object remain an ArrayList. Note that your program runs the same.
6. There are other data structures which also implement the List interface (i.e. LinkedList). Although LinkedList and ArrayList probably don't share any code, they implement the same interface. Note that you can conveniently change your object type from ArrayList to LinkedList because they implement the same interface.
7. So one common thing is for Java to use an Interface to specify some common methods. We can have 2 totally separate hunks of code which both implement the interface. This is a very common thing for Java to do.

Sample Programs from Video


// Each of the following needs to be in a separate file
//**********************************************************

package life;

public interface Living {
    public void move();
    public void eat();
    public void born();
    public void die();
}

//**********************************************************

package life;

public class Snake implements Living{

        public void move() {
            System.out.println("I like to slither on the ground");
        }

        public void eat() {
            System.out.println("I like to just open wide and suck my prey in whole ... yumm");
        }

        public void born() {
            System.out.println("I once was hatched as an egg and had to break my way out. ");
        }

        public void die() {
            System.out.println("People who are afraid of me sometimes smash me with shovels ");
        }

    }

//**********************************************************

package life;

public class Cow implements Living{

        public void move() {
            System.out.println("I walk on all fours");
        }

        public void eat() {
            System.out.println("I like chew my cud");
        }

        public void born() {
            System.out.println("I once was born as a calf ");
        }

        public void die() {
            System.out.println("People who like hamburger usually do me in. ");
        }

    }



//**********************************************************

package life;

public class Tree implements Living{

    public void move() {
        System.out.println("I really don't move, but I do sway");
    }

    public void eat() {
        System.out.println("I suck nutrition from my soil and take in light from the sun");
    }

    public void born() {
        System.out.println("I was born from a seed that dropped one day");
    }

    public void die() {
        System.out.println("Sometimes you cut me down, sometimes I can live a long time. ");
    }

}


//**********************************************************

package life;

public class GameOfLife {

    public static void main(String[] args) {
        Living[] livingArr = new Living[5];

        livingArr[0] = new Tree();
        livingArr[1] = new Cow();
        livingArr[2] = new Snake();
        livingArr[3] = new Tree();
        livingArr[4] = new Snake();

        for (int i=0; i < livingArr.length; i++)
        {
            System.out.println("Item: "+i);
            livingArr[i].born();
            livingArr[i].eat();
            livingArr[i].move();
            livingArr[i].die();

            System.out.println("********");
        }

    }

}




List interface examples: ArrayList, LinkedList


package simple_data_structures;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Person
{
    int age;
    String name;
    double height;
    Person(String n, int a, double h)
    {
        name = n;
        age = a;
        height = h;
    }
    public String toString()
    {
        return name + " " + age + " " + height;
    }
}
public class TestDataStructures {

    //Start out with ArrayList across the board.
    // Then make the datatype be List
    // Then make the class type be LinkedList

    public static void testArrayList()
    {
        // List aso = new ArrayList();
        List aso = new LinkedList();
        aso.add("red");
        aso.add("blue");
        aso.add("green");

        //List<String> as = new ArrayList<String>();
        List<String> as = new LinkedList<String>();
        as.add("red");
        as.add("blue");
        as.add("green");



        for (int i=0; i < as.size(); i++)
        {
            String s1 = as.get(i);
            String s2 = (String)aso.get(i);
            System.out.println(s1 + " " + s2);
        }

        // List apo = new ArrayList();
        List apo = new LinkedList();
        apo.add(new Person("Fred", 29, 70.0));
        apo.add(new Person("George", 35, 75.0));
        apo.add(new Person("Mike", 21, 63.0));
        apo.add(new Person("Sam", 25, 73.0));
        apo.add(new Person("Ned", 15, 69.0));

        //List <Person>ap = new ArrayList<Person>();
        List<Person> ap = new LinkedList<Person>();
        ap.add(new Person("Fred", 29, 70.0));
        ap.add(new Person("George", 35, 75.0));
        ap.add(new Person("Mike", 21, 63.0));
        ap.add(new Person("Sam", 25, 73.0));
        ap.add(new Person("Ned", 15, 69.0));



        for (int i=0; i < ap.size(); i++)
        {
            Person p1 = ap.get(i);
            Person p2 = (Person)apo.get(i);
            // Note that the Person class has a toString method
            System.out.println(p1 + " " + p2);
        }


    }

    public static void main(String[] args) {

        testArrayList();



    }
}



Last Updated: July 22, 2013 3:04 PM