== Objects and Classes Java is an object-oriented language; it doesn't limit you to the pre-defined types of data (integers, doubles, chars, etc.); it allows you to create new types of data. New data types are called "classes". A class can hold data and can contain functions (called methods) that act on that data. Java comes with many pre-defined classes such as String and you can define new classes; for example: {{{ public class MyClass { int x = 5; static void printX(void) { System.out.println("X="+x); } } }}} NOTE: Class names start with an upper-case letter and must be in a file with the class name (e.g. !MyClass.java) You can create and use new variables (objects) of the !MyClass type: {{{ MyClass myObject; // declare an object variable myObject = new MyClass(); // create object and store in variable myObject.printX(); // invoke a method on the object }}} === Constructors You create an object by calling a special method called the [http://tutorials.jenkov.com/java/constructors.html constructor]. In the example above, the constructor was !MyClass() and is the default constructor that any class has. You can create additional constructor methods for your classes that do things like initializing their data members or preparing hardware they are associated with for use. For example, when creating an xbox object, you would call the constructor XboxController(0); to indicate that you want to use the first (0th) xbox controller. Consider this example: {{{ class Student { private String name; public int averageGrade; public Student(String n, int avg) { name = n; averageGrade = avg; } public static void main(String[] args) { Student s = new Student("John", 9); } } }}} === Class relationships There two main ways classes can be related: * Hierarchical: Classes that are related hierarchically will pass the "isa" test. For example, an Apple isa Fruit. A Car isa Vehicle. * Containment : Classes that are related by containment will pass the "hasa" test. For example, an Apple hasa Seed. A Car hasa Passenger. ==== Class Hierarchy (Inheritance) Inheritance provides a clean method for "factoring" your software. If many objects of the same type share some functionality, it's useful (important) to write and maintain that functionality in one place. With large code-bases, this makes code smaller, easier to write, easier to understand, and easier to repair (if you find a bug, you only need to fix it in one place). For example, you can define a class Fruit and then extend it with sub-classes such as Apple (and Orange, Banana, Kiwi, etc.). Each sub-class meets the "isa" test: an Apple isa Fruit. When a class extends another class, it inherits all of the attributes and functions of the parent (super) class. Create a new folder (!InheritanceExample) and in it create the following java files: Fruit.java: {{{ public class Fruit { int weight; // weight of the fruit (grams) int sugar; // sugar contained (grams) final int sweet = 10; // ratio to be "sweet" (percent) public Fruit(int weight_grams, int sugar_grams) { this.weight = weight_grams; this.sugar = sugar_grams; } // Fruit is sweet if more than 10% sugar public void eat() { if (sugar * sweet > weight) { System.out.println("Sweet!"); } else { System.out.println("Tart."); } } } }}} Apple.java: {{{ import java.lang.String; public class Apple extends Fruit { String variant; public Apple(String variant, int weight_grams, int sugar_grams) { super(weight_grams, sugar_grams); this.variant = variant; } @Override public void eat() { System.out.print("Eating a "+variant+" Apple: "); super.eat(); } } }}} Grapefruit.java: {{{ import java.lang.String; public class Grapefruit extends Fruit { String color; public Grapefruit(String color, int weight_grams, int sugar_grams) { super(weight_grams, sugar_grams); this.color= color; } @Override public void eat() { System.out.print("Eating a "+color+" Grapefruit: "); super.eat(); } } }}} You can then create a program that uses these classes named Inheritance.java {{{ public class Inheritance { public static void main(String args[]) { Apple a = new Apple("Honeycrisp", 100, 12); Grapefruit g = new Grapefruit("Pink", 100, 4); Fruit f = a; f.eat(); g.eat(); // g=a; <<< illegal because an Apple is not a Grapefruit (fails isa test) } } }}} Use the debugger to run and explore the Inheritance program and explore what happens in each line of the program. Be sure to step into functions to see how the code flows between the classes. Examine how the Apple class overrides and extends the eat() method of the Fruit class including: * the use of the @Override notation to indicate to the reader that a superclass method is being overridden * the use of the super.eat() call to invoke the eat method of the extended super class (Fruit) ==== Class containment Sometimes it's useful to model the containment relationship between classes. For example, consider the following classes: Plane.java: {{{ import java.util.ArrayList; public class Plane { ArrayList passengers; int seats; public Plane(int seats) { this.seats = seats; passengers = new ArrayList<>(); } public void board(Passenger p) { if (passengers.size() < seats) { passengers.add(p); } else { System.out.println("Sorry, plane is full."); } } public int weight() { int w=0; for (Passenger p:passengers) { w += p.weight; } return w; } } }}} Passenger.java {{{ public class Passenger { public int weight; String name; public Passenger(String name, int weight) { this.name = name; this.weight=weight; } } }}} Containment.java {{{ public class Containment { public static void main(String args[]) { Plane puddleHopper = new Plane(10); Passenger p1 = new Passenger("Bob", 180); Passenger p2 = new Passenger("Alice", 135); puddleHopper.board(p1); puddleHopper.board(p2); System.out.println("Passengers weigh "+puddleHopper.weight()+" lbs"); } } }}} == Learn More Java There's a lot to programming and you'll get to the fun stuff a lot faster if you do some self-study. There are many Java tutorials, more examples, games, and other Java training material on the [wiki:ControlSystems/SoftwareTeam/Training training pages].