Objects, Methods & Classes

Java is an object oriented language. This means that programs written in Java as based on the software objects.

Let's use a real world example to explain this. Classes are general descriptions of a type of object. For example we might have a class called MusicPlayer. A MusicPlayer has a range of things it can do, or methods. It can skip, change volume and switch on or off. The methods have parameters such as the number of tracks to skip.

based on that we can instantiate or create some music playing objects. Lets put some music playing objects in our house.

First we instantiate the objects:

MusicPlayer kitchenPlayer, bedroomPLayer, loungePlayer;

Then we might initialize them with their starting state:

kitchenPlayer = new MusicPlayer (switch=off,track=4,volume=6);
bedroomPlayer = new MusicPlayer (switch=off, track=1,volume=3);
loungePlayer = new MusicPlayer (switch=on,track=6,volume=10);

Now we can use the methods in the MusicPlayer class to control the music in the house:

kitchenPlayer.switch(on); //turn the music on in the kitchen
kitchenPlayer.skip(+3); //skip ahead three tracks in the kitchen
loungePlayer.volume(-2); //turn the volume down 2 notches in the lounge

Overview of Objects and Classes

For those that might want a little more explanation watch this short video from Lynda.com.

An example in Java

So this is how object oriented programming works. For more on objects, classes and methods refer to the Java documentation.

Objects: https://docs.oracle.com/javase/tutorial/java/concepts/object.html

Classes: https://docs.oracle.com/javase/tutorial/java/concepts/class.html

Packages: https://docs.oracle.com/javase/tutorial/java/concepts/package.html

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License