148 | | ...Coming soon... |
| 148 | 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: |
| 149 | {{{ |
| 150 | public class MyClass { |
| 151 | int x = 5; |
| 152 | |
| 153 | static void printX(void) { |
| 154 | System.out.println("X="+x); |
| 155 | } |
| 156 | } |
| 157 | }}} |
| 158 | |
| 159 | NOTE: Class names start with an upper-case letter and must be in a file with the class name (e.g. MyClass.java) |
| 160 | |
| 161 | You can create and use new variables (objects) of the MyClass type: |
| 162 | {{{ |
| 163 | MyClass myObject; // declare an object variable |
| 164 | |
| 165 | myObject = new MyClass(); // create object and store in variable |
| 166 | myObject.printX(); // invoke a method on the object |
| 167 | }}} |
| 168 | |
| 169 | === Constructors |
| 170 | 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: |
| 171 | {{{ |
| 172 | class Student { |
| 173 | private String name; |
| 174 | public int averageGrade; |
| 175 | |
| 176 | |
| 177 | public Student(String n, int avg) { |
| 178 | name = n; |
| 179 | averageGrade = avg; |
| 180 | } |
| 181 | |
| 182 | public static void main(String[] args) { |
| 183 | Student s = new Student("John", 9); |
| 184 | } |
| 185 | } |
| 186 | }}} |
| 187 | |
| 188 | === Inheritence |
| 189 | Classes can be extended so that you don't need to replicate lots of code. This is particularly useful when there is a hierarchical relationship between classes. For example, you can define a class Fruit and then extend it with sub-classes Orange, Apple, Banana. Each of the sub-classes 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. So for example consider the following classes: |
| 190 | {{{ |
| 191 | class Fruit { |
| 192 | String color; |
| 193 | |
| 194 | void eat(void) { |
| 195 | ...munch munch... |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | class Orange extends Fruit { |
| 200 | int numSegments; |
| 201 | } |
| 202 | }}} |
| 203 | |
| 204 | You could create variables of these class types and use them in interesting ways: |
| 205 | {{{ |
| 206 | Fruit f; |
| 207 | Orange o; |
| 208 | Apple a; |
| 209 | |
| 210 | o = new Orange(); |
| 211 | a = new Apple(); |
| 212 | f = o; // legit because o "isa" Fruit |
| 213 | f = a; // also legit because a "isa" Fruit |
| 214 | f.eat(); // eat the apple |
| 215 | |
| 216 | o = a; // ERROR: a is not an Orange so can't be assigned to one |
| 217 | }}} |