Understanding Classes, Objects, and Methods in Programming
Written on
Chapter 1: Introduction to Object-Oriented Programming
In the realm of object-oriented programming, classes serve as blueprints that encompass the methods and variables associated with specific objects. An object is a distinct instance of a data type defined within a class.
For instance, consider "Human beings" as a class of living creatures. Within this class, you'll find various individuals, such as Muhammad, John, Abdulshakur, and Mary. Each of these names represents an object. Thus, while "Human beings" designates the class, "Mary" is an individual object.
Take a look at the following Node.js example:
const EmitterEmit = require('events');
const emitter = new EmitterEmit();
emitter.on('Logged in', function(){
console.log('You just logged in.');});
emitter.emit('You just logged in.');
In this snippet, EmitterEmit is identified as a class, and the events in Node.js provide various methods like addListener or on. Similar to how we are categorized under the class of human beings, events are encapsulated within the EmitterEmit class.
Notice how EmitterEmit is capitalized, a common convention for class names. The expression new EmitterEmit() instantiates an object, representing a specific type of event, such as the "on" method or an event listener (addListener).
A method resides within a class and executes an action. This action could involve responding to user interactions, such as clicks on a webpage or dispatching messages. For example, the "on" method in the aforementioned code alerts you when you log in.
Takeaway
To summarize, think of a class as the concept of "Human," while an object is akin to an individual, like "Joe." Methods represent the actions Joe can perform—eating, running, sleeping, or sending emails.
P.S. Writers often face hurdles that can hinder their ability to write, such as chronic back pain from prolonged sitting, eye strain from extended screen time, and cramped fingers from typing. If you enjoy this kind of content and wish to support me, consider becoming a Medium subscriber for just $5 a month. A portion of your subscription will support my work.
Chapter 2: Practical Examples of Classes and Objects
The first video titled "Java Tutorials 03: Objects, Classes, Methods, Instance Variables" provides a comprehensive overview of the key concepts in Java programming, focusing on how classes and objects interact.
The second video, "Java Classes & Objects," delves deeper into the relationship between classes and objects in Java, illustrating how they work together in programming.