RoboRIO

Overview: 
Explore the Inheritance feature of Java's object oriented design.
Objectives: 

Understand object inheritance and the concept of extending objects.

Content: 

In our first unit on objects we learned how objects can encapsulate fields and methods creating a custom data type we can use to model the actual objects in the problem we are trying to solve. These objects make it easy to describe and work with our data and facilitate reuse of code. In this unit we are going to explore some of the more powerful features of Java's object oriented design.

The first advanced object feature we will discuss is inheritance. Inheritance simply means that when we design a new object, we can include the fields and methods of some other existing object. We can say our new object inherits or extends the characteristics of the base object.

We inherit or extend an object into a new object by using the extends keyword on the new objects class definition:

When we do this, NewClass is said to be a subclass (or child) of OldClass. OldClass is said to be the superclass (or parent) of NewClass. OldClass may also be called the base class of NewClass. A class may extend only one other class. If a class does not explicitly extend another class, then it automatically extends the built-in Java class Object. Classes may extend classes that extend other classes creating a class hierarchy. All class hierarchies have the Object class at the base. Since NewClass is a subclass of OldClass, the NewClass object can be used anywhere an OldClass object is expected.

NewClass will have whatever fields and methods you write for it and it will also have all of the accessible (public) fields and methods of OldClass (except for the constructors). This idea of inheritance allows us to build new objects reusing existing code by extending or specializing our classes into more specific or customized objects for modeling more specific object data and behaviors.

Lets look at an example. We will start with a class called Animal. It will have some fields and methods common to all animals. We will extend Animal with the more specialized classes Dog and Cat. These classes will have fields and methods that apply only to them but will also have the fields and methods of class Animal:

The Dog and Cat class constructors show how to use the super keyword in a subclass to access constructors and members of the superclass. So you see that the Dog and Cat classes do not have a name member defined in them but the resulting classes do have a name member since it is defined in the parent or super class.

Here is the example above in CodingGround.  Fix the compile error and see the code in action.

One aspect of inheritance is that you can use a subclass any place that the super class is required. In our example, you can use a Cat or a Dog object any place an Animal object is required. If you add this method to the Animal class above:

Then you can to this in the main method:

This example also shows something useful. The Java Object class has a method called getClass(). When called from any class it returns information about that calling class. Since Animal is automatically a subclass of Object, in the println() statement, we call animal.getClass().getName() to get the name of the calling class. You might expect this statement to print out "Animal" for the class, but since we passed in instances of Dog and Cat in place of Animal, you get the name of those classes.

Modify the CodingGroup example and add an animal of your choice and test.

Object hierarchies are very powerful and used extensively in robotics API libraries. Extending classes allows you to build complex classes out of simpler classes and reuse code in classes rather than duplicating that code in other classes.

Finally, with the Tetrix and RoboRio platforms, robot programs don't not have a main method. You will write a class that extends a base class provided by the API library for your platform. In this case, there is already a robot program (the base class) with a main method. You will add your code to the base class by extending it with your first class. You will see this in the lessons on those platforms.

Here is a video about inheritance. Here is a detailed discussion of inheritance. Here is more information about the super keyword.

 

Navigation:

Overview: 
Discussion of how to proceed with the following units depending on which robot hardware platform you are using.
Objectives: 

Determine what Unit to proceed to next.

Content: 

If you are using the EV3 platform, it is simple enough that you have learned enough Java to do some actual programming. You can skip to Unit 9 and start working on some EV3 example programs. As you proceed in the examples you may see Java concepts and constructs that are covered in Unit 8. After doing some of the examples you should return to Unit 8 and complete it as it covers topics that, while not needed to get started, likely will be needed as you write more serious robot programs. After completing Unit 8 and doing the examples you should also study Units 12 and 13 to learn still more about Java, enabling you to do more sophisticated robot programming.

If you are using the Tetrix or RoboRio platforms, you could skip to those Units and start working with the examples. However, you will quickly encounter concepts and constructs covered in Unit 8. If you can't resist starting to program and skip Unit 8, try to return to it as soon as possible, so you will know more about what you will be seeing in the example programs you will be working with. Units 12 and 13 should also be completed to give you a more complete picture of what you will be seeing in the examples and what you are likely to see if you look at the FIRST samples or code from other teams.

Navigation:

Overview: 
Introduction to packages and the import statement.
Objectives: 

Gain a basic understanding of packages and the import statement.

Content: 

Classes within a project or in a library are organized into packages. A package is simply a grouping identifier specified at the top of a class with the package statement. In a project, all classes with the same package name are grouped together under that name. Packages are important when we want to use libraries of classes published by other programmers, such as the Java Class Library or one of the robot specific libraries included in robot SDKs. Packages resolve naming conflicts between classes and help programmers to locate the classes they would like to use in their programs.

So how do we make use of packages? If we want to use an external class in our program, we tell Java which package contains the class we want to use with the import statement. Placed at the top of our program, one or more import statements make the imported classes act like they are part of our program and we can then work with those classes in our code.

You will see various package and import statements in the examples. Packages and importing them are discussed in more detail in Unit 13.

Navigation:

Overview: 
Explore object constructor methods.
Objectives: 

Understand what an object constructor is and how it is used.

Content: 

Objects have special kind of method called a constructor. A constructor is an optional method that is called when a new instance of an object is created with the new keyword. Constructors are used to initialize the fields of the new object instance. A constructor looks like a method except that it has no return data type (including void) and has the same name as the class. A constructor can have a parameter list and a class can have more than one constructor by varying the parameter list. Lets look at our Dog class from the previous lesson:

We see that the fields breed and name are not initialized. This would lead to a run time error (called an exception) if we called the bark() method without first putting something in the breed and name fields. We can fix this with direct initialization of the fields or with a constructor method, and we will add a second constructor that allows us to set the breed and name fields when we create a new Dog object:

 So in another place in your code you would write:

We have added three constructors, the first with no parameters which would create a Dog object with the breed field set to an empty string. We directly set the name field to an empty string. When we create an instance of this class, the bark() method won't fail because the constructor is called and initializes the breed field and the name field is initialized when the object is created by Java. If we use the second constructor when we create the Dog object instance, the breed field is set with our desired value. If we use the third constructor when we create the Dog object the breed and name fields are set to our desired values on one statement.

If you omit a constructor, the Java compiler creates one for you internally. That default constructor has no parameters and no code so it really does nothing.

Note that the second constructor shows the use of the this keyword again and also how you can do it without using this.

Here is a video about constructors.

Here is the example on CodingGround. Modify the example to add an age variable (field) and a constructor that allows you to initialize the age (along with breed and name) when creating a new Dog object instance.

 

 

Navigation:

Overview: 
Explore Reference (object instance) variables.
Objectives: 

Understand the basic concept of reference variables and how we access object instances through reference variables.

Content: 

Having introduced objects and classes we need to discuss reference variables. Classes, which describe objects, can be thought of as complex Data Types. When we create an object instance with the new operator, we put the return value of new into a variable whose Data Type is the class name. This kind of variable is a reference variable.

Unlike primitive data types, which store single data values, reference variables store a reference (or pointer) to the instance (the bundle of data) of an object we create with the new operator. A reference variable is initialized by setting it to a new instance of an object or setting it equal to an existing object instance:

The new operator allocates the memory needed to hold the object instance's variables and stores the location of that block of memory in the reference variable. You have one class that describes an object but could have many instances of the object allocated in your program.

Reference variables allow us to access the variables and methods contained in instances of objects using the . (dot) accessor. For example, all objects in Java have a built-in method called toString(). That method returns a string containing either a description of or the contents of the object, as appropriate. You would call toString() like this:

In this example, the contents of the String object instance pointed to by the variable myString is returned by toString() and that content is passed to the system method println() which prints the string of characters on the system console. This example shows how the output of one method can be passed directly to another method as long as the data type returned matches the data type expected.

Methods, when called, are always specified with a parameter list ( ), even if it is empty.

If an object had a integer variable called age, you could access it like this:

The Java String class mentioned above is a built in class that stores and manipulates strings of characters. Strings are used extensively in Java programs. Watch this video for more about Strings. Java also has built in classes for manipulating numbers, discussed in Unit 12.

Note that the compiler and JVM check reference variable data types just like with primitive data types. If a variable is typed as String, then only a String instance can be placed into the variable. Similarly, if you have a variable of type MyObject, only instances of MyObject can be placed into that variable.

 

Navigation:

Overview: 
Explore the basics of objects and classes.
Objectives: 

Understand what objects and classes are and a basic idea of how to use them.

Content: 

Java is an object-oriented language so it stands to reason that objects are the workhorse construct of the language. We have talked about objects and classes in previous lessons but now its time start our detailed exploration of objects.

An object is a software container that includes state information (variables) and behaviors (methods). Objects are used to model real-world things like cars, people, bank accounts, etc. Joysticks, controllers, Motors, servos, and sensors are all typically going to be objects in robot code.

A class is a prototype or blueprint for an object from which actual object instances are created. You can think of a class as a programmer defined Data Type. Object and class can be used interchangeably and that is ok, but there is a difference. We write a class as source code in a .java file and then use that class definition in another .java file to create one or more instances of the object defined by the class with the new keyword. The basic form of a class definition is:

We will discuss modifiers later, for now we use the word public. This means other classes can access this class. The keyword class is required and the name of the class followed by the class body as a statement block. Here is a simple class definition for a real world object, your dog:

We see the Dog class has state information in the variables breed, name and age. These are also called fields. We have made the fields public so we can access them directly from other code. At this point, we have not created any particular instance of a ‘dog’ object, we have just said “All Dogs will have a breed, name, and age.” The only constraint we have is that the breed and name both have to be strings, and the age has to be an integer.

We also see a method that implements a behavior of dogs. With this class you have created your own custom Data Type called Dog. So in another place in your code you might write:

The new keyword creates a new object instance of the Dog Data Type (class) and puts a reference to that instance into the variable myDog. myDog now points to a Dog object instance, which you can manipulate.

We access the components of the Dog object with the . (dot) notation. We use the myDog variable name followed by a dot and then the name of a Dog class component (field or method) we want to access. So, if we want to use the function bark(), we must state myDog.bark(), as opposed to just bark(). The method bark() is owned by the class Dog, and when writing code that is outside of the class Dog, we must access the method bark() by having a Dog object instance to call it with. With a robot motor, for instance, we would create an instance of the Motor class connecting it to an actual motor, store it in variable motorA and then write motorA.setPower(0.5) to set our motorA object instance to half power.

An most significant feature of objects is the ability to use multiple objects all created from the single class definition:

In a robot example, if we have four motors on our robot, we would create four instances of the motor class.
 
Here is more about objects, classes and using objects. Here are two videos (video1, video2) discussing objects and classes.

Note that, while not required, it is considered good programming practice to make all class fields private. This means that fields cannot be directly accessed with the dot notation. In this case, you must provide methods to set and get the values of fields. The reason is to make sure the values of fields are always under the control of the class methods and so the methods can then depend on the values being correct since no outside code can modify them directly. To follow this convention we would rewrite our Dog class like this:

Here we provide methods to give code using our class access to set and get the name and breed fields.

Note that in the setName() method, we used a parameter called name. If we did nothing about this the compiler would not be able to tell the class level field called name from the method parameter called name. The this. keyword allows us to tell the compiler when we are referring to the class level field. You can think of the this. keyword as meaning "class level".

Following that idea, if we have a variable in a method with the same name as a class level field, the variable in the method "hides" the class level field. This means any use of the variable name refers to the method local variable. If we wish to do this and still need to access the class level field, we again use the this. keyword to tell the compiler we mean the class level field.

We also include a constructor method (more later) that allows us to set the dog name when we create an instance with the new keyword. We would use this new Dog class as follows:

Here are the examples on CodingGround (new). Read below before looking at the examples. Add a Cat class with field name and breed and a method called meow() and call it from the main method.

It is a Java convention to have each class is in a separate .java file with the same name as the class. However it is possible to define a class within another class, thus having more than one class in a single file. This is called an inner class. We are only introducing this idea because the new version of CodingGround does not support more than one source file in an example. So in order to have multiple classes in an example, we have to use inner classes. This is a strange limitation as practically all programming languages support breaking projects into multiple source files. The old version of CodingGround allowed multiple files but has quit working.

 

Navigation:

Overview: 
Explore Methods, what they are, how they are used and how to write them.
Overview: 
Wrap up the method unit with references to other sources of information on Java methods.
Objectives: 

View some different explorations of Java methods.

Content: 

We are going to wrap-up the unit on methods with several resources for you to view:

Here is a video about methods. This video is a good introduction to methods. However the definition of the static keyword in this video is incorrect. Please ignore the discussion of static for now as we will explore static later in this curriculum.

Here is a detailed discussion of methods. Here is another discussion (stop at "Constructors").

Navigation:

Overview: 
Explore what "overloading" is and how it is used with methods.
Objectives: 

Understand the "overloading" concept and how it applies to methods and how to use it.

Content: 

Remember that we mentioned that a method's name and parameter list are called the method's signature and this signature is how Java identifies methods. So it follows that while methods with a different name are definitely not the same method, methods with the same name and different parameter lists are also not the same method. This allows you to use different parameter lists to create variations of a method single name. This is called overloading. Here is an example of overloading:

The calling code can call square() with a double or an integer and the correct method for the parameter data type will be used. Here is an example showing varying the number of parameters:

Calling code can use either method but the second has the option to return the area in meters. Note that the second method reuses the first method so that the basic area computation is only defined in one place. In effect, the second method "adds to" or "overloads" the functionality of the first.

Here is this example in CodingGround. Add new methods to compute volume (with and without the meters option) if a height parameter is supplied. In your new methods, reuse the computeArea() method.

 

Navigation:

Pages