Lab 7: Objects & Classes

Compilation

This is our first lab with Java, so before anything else, let’s talk a bit about how compilation works in Java.

In Java, you write separate .java files, each of which contains a single class. The name of the file must exactly match the name of the class contained within; so a file called Blah.javamust contain a class called Blah.

Java files are compiled using javac (the Java compiler), which results in a .class file for each compiled .java file. The resulting .class file can be executed with the java command. In order for a class to be executed this way, it must define the following function:

publicstaticvoidmain(Stringargv[]){}

This is the function which will be executed when the .class file is executed.

Classes and Objects

As you can see, you declare classes in Java with the class keyword. Classes that are intended to be public must also have the public annotation.

On methods, public means that the method is publicly accessible to other objects. static means it is a “class” method, defined on the class rather than on individual instances. This can be used like so:

Note that this code also shows how subtype polymorphism works! Child is a subclass of Parent (put another way, Child inherits from Parent) and can be passed to any method (like printRole) which takes a Parent type as a parameter! This is subtype polymorphism.

Composition vs. Inheritance

You are likely at some point to hear about the composition as an alternative to inheritance, with someone saying that inheritance is terrible and you should always use composition.

Inheritance and composition model two different class relationships. Inheritance models an “is-a” relationship, while composition models a “has-a” relationship. Here’s an example:

Both Bicycle and Car are TransportMechanisms, but only Car has an Engine. So Bicycle and Car use inheritance with TransportMechanism, while only Car uses composition for an Engine.

We’ll get more into object-oriented design next week, but this is the core idea when it comes to composition vs. inheritance. The right choice is going to depend on what you’re modeling.

Abstract Data Types

You can define abstract data types in Java, which define a particular type of data with a particular interface for modification and access, and which must then be implemented to provide a concrete representation of that data. For example:

In this example, we define an abstract data type Bag, along with two concrete versions of it: ArrayBag and ListBag, which each have their own data representation (Object[] items and List<Object> items, respectively). This is how abstract data types work.

Lab Assignment

For today’s lab, please write a Java program that includes inheritance, composition, subtype polymorphism, an abstract data type, and at least two implementations of that abstract data type. Please also provide an explanation of the relationships between the different classes, and why each relationship is modeled in the way you choose to model it. Provide the explanation as a PDF file, and the code as a single .java file. These are both due next Tuesday by 4pm.