Interfaces are a very important part of Java. They can also be quite confusing. In this lesson, we want to take a look at Interfaces, what they are, their relationship to classes, and how and why we use them.
Link into Object Oriented Programming Concepts Trail: (interfaces) http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
What is an Interface?
I think of an interface as like a contract. If you want to fulfill a contract, you have certain obligations to fulfill. The same for a Java contract (interface). An interface defines a set of obligations that an implementing class needs to provide. The obligations are nothing more than method signatures. No code, just the signatures. The class that implements the interface provides the code.
The other thing an interface does, is it allows different classes to appear similar, at least in so far as they implement the interface. As you will see, a Bird, a Fish, and a Mammal are not the same, but if they all implement the Animal interface, then we can treat them all like Animals.
This lesson will look more at class concepts in Java: Extending, overriding, composition, and inner classes. It was created as a lesson for a class room setting. I have converted it to a post here.
Is a Class just a Class?
I recently heard that the classic contractor response to a question is “Well, it depends.” That might just apply here. There are several different types of classes. There are regular classes, like we have been using, there are inner classes, and there are anonymous inner classes. In all cases though, they behave just the same. They still need to be instantiated, and they still contain variables and methods. What we will look at here is what those different types are, and when you might use them.
To do this, we will once again revisit the Robot. This time we will be adding a head that rotates to the Robot. We will look at placing the code directly into the Robot without using another class, using an inner class to Robot, and using containment to hold another class.
Before we get to that, lets revisit the parent/child relationship that exists when a class is extended and how casting works. Then we will get to the RobotHead.
Sun Trails Index: http://java.sun.com/docs/books/tutorial/index.html
Link into Objects and Classes Trail: (nested classes) http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
This lesson will cover some basic concepts in Java related to static variables and methods. It was created as a lesson for a class room setting. I have converted it to a post here.
What does static mean?
Lucky for us, static has nothing to do with statics, a class I had to take in Engineering about the forces on objects that are not moving.
What is does have to do with, seems to go against the concepts we talked about in the Classes vs. Objects lesson. If you followed along there, we talked about how the Class was the blueprint, and the Object was a concrete, instantiated, “built” Class. Its variables were its own, and the methods belonged to this Object.
When something is static, it means that it belongs to the class, and not the object. Every object has access to this item, but it is not unique to the object.
Link into OO Trail: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
This lesson will cover some basic concepts in Java related to classes and objects. It was created as a lesson for a class room setting. I have converted it to a post here.
Class vs Object
Definition from page 10, Java A Beginner’s Guide (Herbert Schildt)
A class defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specification to construct objects. Objects are instances of a class. Thus, a class is essentially a set of plans that specify how to build an object.
The code and data that constitute a class are called members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods.
OO Trail: http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
I have put up a new section to the site. It is the “code” tab at the top. I plan on putting tutorials or code that I have to share up here. The first thing to grace the new section is a tutorial called Intro to Java Objects and Classes. I wrote this to present at work.