Everything is an Object
What do I really mean when I say that?
I mean that every Class you create in Java actually inherits from the Java Object type.
That’s right Object is a variable type in Java
You can create a variable anywhere in your code and just assign it the type “Object”
- The downside to doing this, is that you limit what the variable will be able to do, as the “Object” variable type is quite generic
So the trick is to create your own Objects. How do you do this? All you do is create a Class, you have done this before when you created your first “HelloWorld” program remember? You just created a “HelloWorld.java” file, that file represents a Class (which is an Object).
The difference between an Object and a Class is that a Class is a blueprint for an Object. Think of a house’s blueprints, you only have one blueprint, but you can make (or create) countless houses from that blueprint. This is the exact same relationship between Class and Object. There’s only one Class, but countless Objects can be created from the Class definition.
So, when you create a Class and you name it whatever you like, that Class automatically inherits the functionality of the Java Object. This is done purposely, because like I said, everything in Java is an Object!
How would you know what Objects to create? Let’s learn from an example, our common example is a User logging into a website. Let’s think of a real world example, like how Users work with JavaVideoTutorials.net.
“A User must be able to sign-up with an email address, username and password. Once they have signed up and confirmed their email address, they will then gain access to the website. With access to the website they will be able to view Videos and take Tests to ensure they are retaining what they are learning.”
When diagnosing a requirement, if you were to isolate all of the nouns (person, place or thing) in the sentence, you would have a good idea of what Objects you would need to create in code. When diagnosing this requirement, I pull out the following nouns:
- Email Address
Now, we could create 7 different Objects here, BUT, that wouldn’t be a very efficient design. The trick now is to determine relationships between these “nouns”. We do this by defining two relationships known as “is a” and “has a” relationships.
is a Relationship
There are no “is a” relationships in our example, but I’ll give you a typical example of this relationship. Let’s say we have a bunch of Objects: Vehicle, Car, Motorcycle and Bus. Given these Objects, can you identify
is a relationships? Well, can you say that a “Motorcycle is a Car”… no, but can you say a “Motorcycle is a Vehicle”… YES! Also a “Car is a Vehicle” and a “Bus is a Vehicle”. This is very useful for something called inheritance, but I’ll talk about that in another episode.
has a Relationship
In our example, can we say that a “Password has a User”? Not exactly, but we can say a “User has a Password”, we can also say a “User has a Username and Email Address”. When we identify this relationship, it means that these Objects that the User “Has” should be properties of the User Object. This means that when we are dealing with User objects, we can retrieve the Password, Username and Email Address from a particular User Object. The same relationship can be found with Videos and Tests, we can say that a “Video has a Test”, so that means that the Video Object should have a “Test” property!
Once you dissect your requirements you’ll be able to identify all the Objects and you’ll also be able to have a really good idea of what the Objects should look like (i.e. what properties they should have)
Read the following articles: