← Back to list

Java — week 2

In this week, I learned about a few concepts related to classes and methods. As I have already coded in C++ and es6 Javascript, class is a…

Akshaya K L · 2023-09-11 14:39 · 80 claps · 4.1 min read
#java #head-first-java #oops-concepts #inheritence #abstraction
Open on Medium ↗
Wiki topics: TLS · Design Tools & Workflow 🌐 · Web Development

Java — week 2

In this week, I learned about a few concepts related to classes and methods. As I have already coded in C++ and es6 Javascript, class is a familiar friend. Now in Java, we write our source code in a .java file. The .java file gets compiled into a .class file. When JVM runs this file, it essentially runs a class.

For example, if I have a class called Week2ofJava, when JVM tries to run this file after compilation, it searches for the main method in the file to execute it. In other words, no matter how long my code inside the class is, we need the main() function to get the code started. (it’s called “main” for a reason)

As we can see, everything in Java lies in a class. This raises a fundamental question as a javascript developer, whyyy? The reason for this is that Java unlike javascript is an Object Oriented Language. (Don’t come at me saying Javascript can also be claimed to be an Object Oriented Language. While I don’t deny that, I feel Javascript is primarily designed as a functional language and doesn’t strictly implement OOPs concepts while it tries to embrace them). Now, what really is Object Oriented Programming?

In the world of Object Oriented Programming, everything is an object and it can have certain properties, can perform certain actions. Every object can have a set of hidden properties, that only its children can inherit and every object can have a set of properties that no one knows. Well, it feels scarily accurate to human beings, right? These objects can be used to describe a multitude of things, human beings, of course, can be one of the infinite possibilities.

Let’s take an example, let’s say I want to define a book as an object. A book can have certain properties — number of pages, genre, author, price, rating, etc. Can a book do something? books being non-living things can’t do much (well, some books can even change your life). So do methods have to be actions that could be performed by the book? Yes and No. That depends on how you decide to use methods for this object. I could also define methods that are performed on a book as a method of the object book. I can choose not to do that as well.

Coming back to the main() method. Do I have to write a main() function in every class I write? No! We just need one main() method to get the program started

Let’s look at a real-life example to get us started with the comparison between Object Oriented and Procedural Programming please check the original version (with cool images)in the book (Head First Java) which I’m using for reference.

Let’s say you work as an intern in a company called Potions and your manager Snape, gives a piece of work to 2 of you (you(Harry) and Draco). Now, Snape wanted a code to brew potions based on a set of parameters. He tells you, that the base ingredients are lard, leech juice, ginger juice, and lavender.

Preparing a potion involves 4 steps — gather ingredients, prepare the ingredients for brewing, adjust the environment conditions for brewing and brewing.

We need to prepare 4 potions, for the lack of creative naming— red, blue, green, and yellow.

You would not have normally picked this task up, but Snape told you that there is just one vacant position for an intern and this task would be a deciding factor.

You being Harry, tried to get the easy way out and approached Hermione. Hermione helps you out by saying that you will use the Object-based approach — you will create classes for each potion and mention the methods in them. So, you do exactly the same.

Draco, on the other hand, wrote a procedural program — A program that included methods.

Draco finished his code in no time and smirked at the number of files that you have written, his code was concise and probably would have won Snape’s heart. But Dumbledore wanted a magenta potion and urged Snape to make it quick. Snape then added that as a requirement with a lot of grunting. Making the magenta potion involved a human eye as an ingredient and how exactly would you gather it? (I’ll leave it to your own imagination) and it also needs flesh. There was also the process of preserving them and preparing them for brewing.

You created a new class without touching the existing classes' code and test cases. While Draco had to make modifications to the existing code requiring test cases to be rewritten, You just had to write tests for a new object, Yay!

You finished your code on time, but before deploying it, Draco pointed out, “You have written so many duplicate functions!”. You corrected him, you said that they were methods and you had actually written an abstract class called potions that had all the default abstract methods and every other potion has inherited the methods from it.

(You obviously learned that from Hermione)

Draco: What if a potion has a distinct method different from its “parent”? Something that is not generic?

Harry: We can then override it with the same method name defined in the child class.

So against all odds, Snape, with a broken heart had to deploy your code and implore Dumbledore to open 2 positions for interns. Dumbledore, on the other hand, sat wondering about why on earth just adding an extra color took so much time.

Until next week.


메타데이터
post_id
8d230e82c79b
slug
java-week-2-8d230e82c79b
url
https://medium.com/@akshayakl/java-week-2-8d230e82c79b
canonical_url
https://medium.com/@akshayakl/java-week-2-8d230e82c79b
author_url
https://medium.com/@akshayakl
status
ok
fetched_at
2026-08-29 08:11:30