Understanding Java Classes and Objects with a Unique Twist

Class and Object in Java: Make Sense!

Kossei Necira
JavaToDev
5 min readAug 24, 2024

--

Class And Object In Java make sense.

In object-oriented programming with Java, one of The main concepts that any developer must grasp is the concept of “Class”.

Many developers use object-oriented languages, they work with classes and objects in their projects, but not all fully understand what is a class and object, the purpose, and the context of using it. As a result, they either treat every detail as a class, calling things classes even when it’s unnecessary, or they avoid using classes when they really should.

In this article, I’ll do my best to clarify the concept of Java classes using real-world examples. This approach should help you better visualize and understand what classes and objects are when working with them.

Understanding what is a class and object in Java?

What is a class in Java language?

A class in Java is a structure that represents a real-world entity, it simplifies representations of things around us.

What is an object in Java?

An Object in Java is a copy of a Type we define using the class structure.

Okay! maybe you didn’t get it by this definition!

So let me tell you a little story that will help us understand what Java class is really about.

As you may know, Object Oriented Programming is a programming paradigm, it’s an approach! that developers follow to build their software, also you can say it is a way of thinking about software functionality.

but you may ask! before that Paradigm was invented, what was there?

I will tell you :)! the most famous paradigm to build software back then in the 1950s was the procedural paradigm, using programming languages like Fortran and Algol, Which is a bunch of functions that call each other to make the program function so that the program fulfills the tasks for which it was built.

You may ask, Why are you telling me this? it’s irrelevant!

Me When I created my first java object :)

Hmmm! this is not accurate! everything in the Software development world is about problems and solutions, procedural programming has many cons for the big thinkers! which are the computer scientists! so they started to think about how to solve those problems like software complexity and scalability issues.

How does a scientist looking around look like :)

They Looked around us and said: wait a minute, Everything around us is an “Object” right?, An object of “Type” of Something more general! a copy!, the meeting room, the rounded table in it, the parrot that repeats everything I say in the hallway, us!

At one point they said: hmmm! every object being a type of something more general must have the same characteristics of its type!

Object Characteristics in Java:

Every object must have 3 characteristics:

  1. Object Identity (Title): so we can recognize and distinguish it from other objects. I used “title” not “name”, so we don’t confuse it, since we are talking abstractly, the title here represents the Type itself.
  2. Object Properties.
  3. Object Functionalities.

Example:

Let’s say we want to define a human being:

Human: is the Identity (Title). (Note: “is” not “has”).

Humans have properties: first name, last name, height, weight, skin color, hair color… etc.

Humans have functionalities such as walking, listening, seeing, and communicating.

To represent a Human in the computer world, we need a structure to encapsulate these functionalities. In the context of Java and many other programming languages, we use a “Class” to represent such entities, making this term quite fitting for the context.

Now, we can make as many Objects as we want of Humans: me! , You Mr./Ms. reader :) !…etc. Objects take characteristics of type Human.

How to define a Class in Java:

At this point, defining a Java class should be straightforward. Based on our earlier discussion about what a Java class represents, you can apply these concepts to code. Here’s how you can create a Java class called Human in your IDE:

Note: in this example, we are not following best practices in defining classes. this example is just for demonstration.

Create a New Class:

Open your IDE and create a new Java class named Human.

Define Properties:

These are the attributes or characteristics of the class. For a Human class, properties might include attributes like first name, last name weight, height…etc.

Define Functionalities:

These are the methods or behaviors of the class. For a Human class, functionalities might include methods like walk(), listen(), see(), and communicate().

class Human {
// defining properties
String firstName;
String lastName;
double height;
double weight;
String skinColor;
String hairColor;

// defining functinalities1
public void walk() {
System.out.println("Walking...");
}

public void see() {
System.out.println("seeing...");
}

public void listen() {
System.out.println("listening...");
}

public void communicate() {
System.out.println("communicating...");
}
}

How to instantiate an object in Java:

instantiating is very easy: In Java language, we use the “new” keyword to create a new object.

So, in the main method or in any other class, we define a new variable of type Human and then we use the new keyword to create the object.

class Main {
public static void main(String args[]) {
// Object instantiation of type Human
Human human1 = new Human();
// properties initialization
human1.firstName = "Ahmed";
human1.skinColor = "brown";
// calling methods
human1.walk();
human1.see();
}
}

Conclusion:

In this article, we explored what is a class and object in Java, starting with a historical perspective and presenting the concept through a story to help you better understand it. A programmer with a solid grasp of the concepts they use is far more effective than one who just writes code and hopes for the best.

Follow me for more articles in the future! If there’s a specific topic you’d like me to cover, feel free to comment, and I’ll be happy to write about it.

If you found value in the article, please consider giving a heartfelt clap 👏 to the original author to show your appreciation for their dedication and hard work.

Connect with us on JavaToDev’s LinkedIn for more updates and lively discussions. Explore additional resources and articles on the official JavaToDev website.

Thank you for your ongoing support and here’s to another year of collaborative learning and growth in Java development!

--

--