Introduction to OOP in Java (Coding Bootcamp series)

If you have been following my “From journalism to coding bootcamp” series, you know that in the previous article I mentioned we started learning OOP (object-oriented programming) here at Green Fox Academy. The past two weeks were devoted to deepening this knowledge and putting it into practice.

First of all, what is OOP?

OOP is a programming language paradigm that uses objects and classes as its core components, allowing the programmer to think in terms of real-life objects. This makes the code cleaner, reusable, maintainable and scalable.

So, if you are a newbie in the area (just like me…) here are the fundamental principles you need to know about Java OOP.

Inheritance

This is the OOP principle that allows classes to derive from other classes. Often times, a program includes several classes that are very similar to each other — but not entirely the same. OOP programming allows us to create a parent class with the common attributes and children classes that define the specific properties.

Let me give you an example with an exercise I completed this week.

“The task is to create a garden application that includes flowers and trees.

The Flower -needs water if its current water amount is less then 5 / when watering it the flower can only absorb the 75% of the water

The Tree -needs water if its current water amount is less then 10 / when watering it the tree can only absorb the 40% of the water”

So, as you can see, both flowers and trees have similar properties which are not, however, the exact same. Inheritance offers the solution. You can create a parent class — let’s call it Plant — just like this:

… and then create two children classes — Flower and Tree — that define the specific attributes, just like this:

Encapsulation

Java classes communicate with each other and are meant to use each other’s fields. What is not meant to happen in most cases, however, is that a class can change a variable of another class. And this is where encapsulation comes in handy.

Let’s get back to the garden example… when we create a Plant class, we create field variables, such as the ones listed below.

They are all set to private, just so they cannot be changed in other classes inadvertently. But what if we need to use them in another class? Maybe we need to check the color of the plant, for example. For that, we can make use of (Setters and) Getters.

Abstraction

Abstraction helps reducing complexity. It gives the users only the relevant information, without letting them know the complex mechanisms behind.

Think of a coffee machine. You put in the coffee beans, you press the button and the coffee starts pouring into your cup. Do you know what is going on inside? No clue. But it’s the end result that matters.

Abstraction in programming is based on this idea and it is achieved making use of abstract classes and / or interfaces.

An interface is similar to a class but different in the sense that it cannot contain the implementation of methods, only the signature. Let’s see the example below:

Code provided as example at Green Fox Academy

Polymorphism

Polymorphism is the ability to take new forms. Remember the inheritance principle we talked about above? Now imagine a parent class has a method that is common to all its children classes but specific to each one. What would we do? Something like this?

And this will be the outcome: “Bass Guitar, a 4-stringed instrument that goes Duum-duum-duum”.

The end of week 6 at Green Fox Academy’s bootcamp was also the end of the first part of the course. We had an exam — I passed, yay! — and this week we will start the Orientation phase, where we will learn about Spring.

See you in two weeks!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close