A Beginner’s Take On Entering The World of Object Oriented Programming in Java

Let’s take a deep dive into OOP with Java

Nafisa Nawrin Labonno
8 min readOct 6, 2024

How many a time did you hear yourself scream “SCREW THAT SEMI-COLON! My program would be PERFECT in Python!!” Well, bad news, amigo. Java is also a member of the “semi-colonophillia” family (I made that name up) which means, like C, Java hates it when you miss a semi-colon :/

https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.dreamstime.com%2Fsic-vita-est-den-ancient-latin-saying-meaning-%25C2%25ABsuch-life-made-metallic-letterpress-dark-jeans-background-image152950136&psig=AOvVaw2Ij_E_u8pWJpLBGAJV0KnA&ust=1728274402355000&source=images&cd=vfe&opi=89978449&ved=0CAMQjB1qFwoTCNiV-f_x-IgDFQAAAAAdAAAAABAS

All you ; lovers feelin’ a little smug right now, huh?

Let me share with you a sneak peak into my journey with OOP thus far. So, I started learning C in college in my freshman year. Now as a sophomore, I am learning Java, which, to be frank, has been a wild ride so far. If you find yourself even remotely being in a similar boat, buckle up for a ride of relatable memes that do justice in capturing novice learners’ emotional damage AND actually getting some *coughs* learning done. Here we go!

From Reddit

That’s right. Several mixed feelings with Java so far:

SYNTAX sucks: Well, if the length of Java codes scares you, you are probably an average human being. If it does not *dialing 911.* Nah, you might be a rare breed and portray Java in a uniquely beautiful way!

Confusion: Aside from its rather long syntax, Java sounded like a confusing language that’s trying to be an overachiever when I started learning it, which, isn’t wrong per se but from a novice’s perspective, it feels like a lot. I had to resort to my class lectures, online learning, email instructor, spend countless nights scrolling through Reddit, Stack Overflow, ChatGPT, ask peers- you name it. But this is far from enough and far from the “right track”. While these are steps towards the right track, one important aspect I often overlooked was to actually implement code. Just watching lectures/learning materials is never enough. And boy, the imposter syndrome is real but with adequate help, it is totally workable. I mean, a few weeks ago I used to think programming and I are an odd pair (still do, but these newfound layers of understanding have made the journey much more enjoyable now).

Get Your Basics Right: In any language, getting your fundamentals correct is key to understanding and appreciating the underlying coding principles and logic that governs what you want to achieve by writing that code. In other words, know what you are doing and know it extremely well. One of my mathematics professors shared his insights into being a self-taught learner throughout his undergrad and grad years such that he made it a mission to never let his PhD advisor question his understanding of the materials when he walked into a class ever again, after a series of realisation. We all go through this phase one time or the other. Especially in this ultra fast-paced world where everything is practically open sourced, knowing how to do things is not expected to be difficult. In Java, when I started testing, packages, it got exponentially more difficult to make connections because I relied upon homework assignments solely. To all my fellow novices out there, do not make this mistake.

Walk into every class knowing your materials and make the most out of your lectures by asking your professors the right questions.

(There’s no wrong question, unless it’s inappropriate. Ask for better clarity! My OOP professor’s take on questions is that he and his TA’s absolutely love them. Big Shoutout!).

Appreciation: At least, one good thing out of (infinite minus n) things about Java is that, IT DOES WHAT IT SAYS!! I’m almost in tears recalling dynamic programming which wasn’t terribly bad in C except, my data structures and algorithmic understanding is in shatters. Time to fix so many things simultaneously… (I love C, don’t get me wrong. Getting to see and learn about the insiders of programming aka Algorithms & Data Structures is something I found to be exceptional and although overwhelming initially, it makes a lot more sense when you ask yourself a simple: “why?” So try asking why whenever you get the chance to and see how clear your understanding becomes.)

https://www.google.com/url?sa=i&url=https%3A%2F%2Fknowyourmeme.com%2Fmemes%2Fcrying-cat&psig=AOvVaw2db-fEOmTL2zNBst5UvROf&ust=1728274742990000&source=images&cd=vfe&opi=89978449&ved=0CAMQjB1qFwoTCJi-oKrz-IgDFQAAAAAdAAAAABAN

So, how is this blog about Object Oriented Programming? This feels like a personal experience sharing without flexing much programming muscles, right? Here we go for the second time, you ungrateful lads & ladies (emotions redacted😕). Just kidding! Sharing both sides of the story anyway!

Java’s fundamental concepts in OOP can be summarised in the following, of which I will be discussing a few in this blog:

  • Classes and Objects
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Additionally, I’m linking some useful blogs/websites/videos for you lot to learn and correct me if you see me make any mistake.

https://learn.oracle.com/ols/learning-path/java-fundamentals/55593/55578

https://www.reddit.com/r/learnjava

Classes and Objects

These are basically the building blocks used in Java to express the language’s functionality. A simple analogy would be to think of Class as Animals OR Plants OR Food/Cuisine but this cannot be a specific element of the class family.

Imagine a class represents animals in a farm. Objects would, therefore, mean the particular kind of animals that reside in the farm. Example: Class Animal can have Objects Cow, Goat, Lamb, Buffalo, etc. You get the idea. So to backtrack, we think of class as a template or blueprint that guides us to create objects in Java. The objects are the ‘instances’ of a class. Instance here means a real-world entity that has the properties or behaviours defined by the class.

The code goes something like:

public class Animal {
String name;
int age;
String colour; // Strings, ints etc. are the data types that
// define your variables like name, age, colour of the class Animal. These
// variables are representative of the attributes of the class Animal
// which will be used to create the objects in the next part of the code.

// Constructor to initialise the attributes of the Animal class
public Animal(String name, int age, String colour) {
// Use 'this' keyword to refer to the instance variables
this.name = name; // Initialise the name attribute
this.age = age; // Initialise the age attribute
this.colour = colour; // Initialise the colour attribute
}

// Method to display information about the animal
void displayAnimalInfo() {
// Print the details of the animal
System.out.println("Animal Name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println("Colour: " + this.colour);
}

// Method for the animal to make a sound
void makeSound() {
// Print a message indicating the animal makes a sound
System.out.println(this.name + " makes a sound.");
}

// Method for the animal to eat
void eat() {
// Print a message indicating the animal is eating
System.out.println(this.name + " is eating.");
}

// Method for the animal to sleep
void sleep() {
// Print a message indicating the animal is sleeping
System.out.println(this.name + " is sleeping.");
}
}

// Class to demonstrate the use of the Animal class
public class Farm {
public static void main(String[] args) {
// Create objects of the Animal class
Animal cow = new Animal("Bessie", 5, "Black and White"); // Create a cow object
Animal goat = new Animal("Billy", 3, "Brown"); // Create a goat object
Animal lamb = new Animal("Dolly", 1, "White"); // Create a lamb object

// Call methods for each animal object to display information and actions
cow.displayAnimalInfo(); // Display cow's information
cow.makeSound(); // Cow makes a sound
cow.eat(); // Cow eats
cow.sleep(); // Cow sleeps

goat.displayAnimalInfo(); // Display goat's information
goat.makeSound(); // Goat makes a sound
goat.eat(); // Goat eats
goat.sleep(); // Goat sleeps

lamb.displayAnimalInfo(); // Display lamb's information
lamb.makeSound(); // Lamb makes a sound
lamb.eat(); // Lamb eats
lamb.sleep(); // Lamb sleeps
}
}

This blog is already long so I will try to share more on the next topics in line in my next blog.

Class: The Animal class represents an animal on a farm with attributes like name, age, and colour.
Constructor: The Animal(String name, int age, String colour) constructor takes the name, age, and color as arguments and initialises the instance variables.
Method: The displayAnimalInfo() method prints the details of the animal.
Main Method: The main method creates different Animal objects using the constructor and then calls the displayAnimalInfo() method to display each animal's details.

  • Fields (variables) define the state of an object, while methods define the behavior of that object.
  • Methods (behaviours) are functions defined within the Animal class that can access, modify, and return field values, allowing for interaction between the state (name, age, colour) and behaviour of objects. e.g., makeSound(), eat(), and sleep() express what an animal can do

Key Characteristics of Constructors:

No Return Type: Constructors do not specify a return type (not even void). This differentiates them from regular methods.

Same Name as the Class: A constructor must have the same name as the class it belongs to.

public class Animal{
// ----> here goes data members
}

public Animal(String name, int age, String colour) {
// Initialise the attributes
}
// The second Animal(String name, int age, String colour) is our constructor
// Everything in the parentheses are the parameters with given types

Called When an Object is Created: Constructors are called when you create an instance of the class using the new keyword.

Can Have Parameters: Constructors can take parameters to initialise (using an = operator) an object with specific values.

I might have taken too long to explain something very simple yet powerful that helps us start with the right footing in OOP. In my next blog, I will try to go briefly over encapsulation & inheritance alongside sharing what I learned so far, In Shaa Allah (if God wills in Arabic).

Thank you very much for reading! Cheers~ Happy coding & please feel free to leave any feedback. Appreciate your time :>

--

--