Learn to Code via Tutorials on Repl.it

← Back to all posts
LLC Java - Learning about OOP
TheDrone7 (545)

LLC Java - OOP Concepts Part 1

What is OOP (Object Oriented Programming)?

I believe there is no point in learning something without even knowing what it is and what are it's good and bad aspects. So here we are learning about OOP. In simple words - OOP is the practice of breaking down your code into parts called Classes which can then be used to create Objects in the future that share similar properties. In more technical terms or you could say as the official definition of OOP -

Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure.
In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.

For example, relating to real life - Classes are like general categories of stuff while Objects are the specific things that we see around us. Like - Consider tables, we have different types of tables in our house - dining table, study table, etc. In this case the Table would be a class defining a general category with Dining Table and Study Table being Objects as they are one of a kind. I will continue using this example in the rest of the tutorial as well.

Each class has two types of components: -
1. Attributes - These are the properties of a class. For example as in our tables example we could have attributes like - the material of the table, the dimensions of the table, etc. In terms of programming these are also called fields. These are usually data belonging to a class defining it's properties.
2. Behaviour - These are the components that define the actions that can be performed on the object. For example in our tables example, the tables could have behaviours like moving and flipping, etc. In terms of programming, these are also called methods. These are usually functions that modify the object's fields to redefine it.


A simple example

A really great example for understanding how to create objects from classes would be the Strings. We create strings in Java by writing code similar to this -

	String someString;

What this line of code actually does is create a new Object for the String Class. The String Class comes pre-defined in Java along with a lot of other classes like Int, Float, Char, Double etc. And we use them very often in our programmes as well.


Getting started

So now that we know the basics about OOP, we can get started creating our own class and objects. As I said earlier, we will be using the tables examples.

Create a new Java repl if you haven't already created one.

Firstly, create a new file named Table.java which will hold our Table class.

class Table{
	String type, material="wood";	// The type of table - dining, study, coffee, etc.
	Double length, width, height;	// the dimensions of the table.
	Double positionX = 0.00, positionY = 0.00, positionZ = 0.00; // The position of the table.
}

For now our class only has a few attributes which are: -

  • type : the type of the table.
  • material : The material of which the table is made up of. (defaults to wood)
  • length : The length of the table.
  • width : The width of the table.
  • height : The height of the table.
  • positionX : The position of the table along the X-Axis.
  • positionY : The position of the table along the Y-Axis.
  • positionZ : The position of the table along the Z-Axis.

Now that we have a basic table class for us, let's create a new object. For that we will jump back to our Main.java file and then in the public static void main function type the following code: -

Table tableOne;		// Create the tableOne variable.
tableOne = new Table();	// Create a new object of the Table class and assign it to the tableOne variable.

/*
	We could also shorten the above two lines into one line, that is: -
	Table tableOne = new Table();
*/

And there we have it, the first object of the class we made ourselves! Just to make sure that it works properly, let's try printing the object.
Write down the following code below the above code

System.out.println(tableOne);

You should see something similar to [email protected] this shows that the variable we tried to print was an object of the Table class.

Let's try printing some of the properties of the table. To access a property, all you need to do is use a period(.) after the object's variable name and you then type the name of the property. For example, let's print the material of the table: -

System.out.println("Table material: " + tableOne.material);

When you run it, you should see Table material: wood in your console.


Adding behaviour

Now that we have an actual table with us, let's make it more real by adding behaviour to it. Our first method will be a method to move the table to another location defined by it's x, y, z co-ordinates. So here we go. Open up the Table.java file and inside the Table class below the definition of the attributes, type down the following code.

/**
	A method to move the object to a specific position.
	@param x The x co-ordinate of the new location
	@param y The y co-ordinate of the new location
	@param z The z co-ordinate of the new location
*/
void moveToPosition(Double x, Double y, Double z) {
	positionX = x;
	positionY = y;
	positionZ = z;
}

Let's see what's going on up there: -
/** ... */ is a special type of multiline comment that we use above classes or functions to help us generate docs later using the Javadoc tool.
Next, we define our function which takes in 3 parameters: -
1. The x co-ordinate.
2. The y co-ordinate.
3. The z co-ordinate.
Then inside our function we tell the compiler to change the current object's co-ordinates to the ones provided by the user. Calling methods is similar to accessing properties, use a period(.) after the object variable name and then name the method to call, except, you will need to use parantheses(, ) to call them just like you would with a normal function along with passing the parameters, if any.

Let's test our new method. Here we will only change the x co-ordinate.
Jump back to Main.java and inside the public static void main write down the following code: -

Table tableOne = new Table();
System.out.println("Original Co-ordinate: " + tableOne.positionX);	// Print the original to see the difference.
tableOne.moveToPosition(12.0, 0.0, 0.0);							// Move the table
System.out.println("New Co-ordinate: " + tableOne.positionX);		// Print the new co-ordinate.

So as we can see, we pass on 3 Double values when calling the method. As an assignment for those who are new, try adding more properties and methods to it to make something new. You can also learn more about OOP with Java [here].


That'll be all for now.

Please upvote the post if it helped you at all.
Thank you.