Java For Beginners #1 – Understanding Java and OOP

Sep 29, 2015
avesdesign

If you’ve already learned some object oriented programming language, then you’ll probably have no problem learning and understanding Java, but if you’re a complete beginner to programming or if you’ve worked only with procedural programming then hopefully this article will give you some insight about OOP languages and what you can expect and do with Java.

If you want to learn and to be able use Java successfully, the first thing that you have to do is to understand how exactly Java works. Not learning all the basics is a mistake that many beginner programmers make, so don’t do it. It’s best to learn and have an idea of how each component of Java works rather than feeling lost halfway through the material and start again from the beginning.

Java For Beginners #1 - Understanding Java and OOP

Understanding Java and OOP (Object Oriented Programming)

As you already know by now, Java is an object-oriented programming language. But what does object-oriented means? Object-oriented programming languages mainly focus on data and the first thing that they do is organize the data and then use commands to tell the computer what to do unlike procedural programming that basically gives the computer commands of what to do.

Because Java is an object-oriented language it means that it can organize the data and enable you to use and modify it however you want without having to change the whole program. Confused? Don’t be, next you’ll learn more about objects – how to create and use them.

Java and OOP – Objects and classes

In object-oriented languages an object is basically a component of a program that knows how to interact with other elements of the program and knows how perform certain actions. Let’s explain things a little bit better. Take for example a cat. The cat has certain characteristics and properties for example a name, breed and color. These are called instance variables or state and these are the things that the object knows about itself. The cat can also do things like meow, purr and eat. These are called methods or behavior and these are the things that the object can do.

A class is a blueprint of a certain object and you have to have a class before you can create an object. Basically the class tells the Java virtual machine how to create the object. Let’s go back to the cat. If you want to create a cat object, you first need to create a class called ‘cat’ and you have to describe the properties of the cat and what the cat does.

The good thing about objects is that they can interact with each other and do more complex stuff. For example if you have a cat object and an automatic feeder object, you can make them interact so the feeder can dispense a certain amount of food and the cat can eat from the feeder.

Classes are extremely useful when you need to create many objects with the same or similar properties. For example, if you want to create 3 cats, rather then describing and creating a class form scratch for each one of them, you can use the basic ‘cat’ class for each one of them. Of course, you can add additional details for each one of them like different name etc but the basic properties and actions will be the same as the class ‘cat’.

Java and OOP – Methods and functions

It was mentioned above that methods are the actions that the object can do. A method describes the behavior of the object and it’s always associated with a class. The connection between class and method is also called binding. Let’s go back to the previous example of a cat object with a ‘cat’ class. The methods connected with the class will be the actions that the cat can perform like meow, purr and eat.

Functions are set of instructions that do something to reach some result. They usually require some arguments (input) and returns result. The difference between a methods and functions is that functions are not associated with a class, they can be independent. You can basically write a function and use it anywhere in your code without having an object that uses it.

So in summary, object-oriented programming enables you to write different segments of code that you can reuse and modify, without having to trash all of your code and start from scratch when you have to add or do some major modifications (like with procedural programming). Hopefully this clears things up a bit about Java and OOP. If you want to dive into Java, check out the second part of Java for Beginners where we talk about data types, variables and arrays.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *