OOP Series — What is an Object?

Hello there. This post will be an introduction to the OOP series. In this series, we will talk about Object-Oriented concepts. As I mentioned in the previous post, you do not need to know a specific programming language or modeling language. After learning the concepts you will gain skills to apply to your favorite OO programming language. But for the sake of better understanding, I will give UML examples and some code examples in Java. But you are free to choose your favorite programming language and try these examples. But before, let’s talk about the fundamentals of OO concepts.
What is an Object?
In OO concepts, objects are defined by two components attributes and behaviors(behaviors are referred to as methods). What if I told you, you are an Object? Can you define your attributes and behaviors? For example, what is your name and how old are you? The answers to these questions are your attributes. And how about your behaviors? You can eat, read or if you are ‘chosen one’ you can fly? So we can think of you as an object. In OOP paradigm we are contained attributes and methods in a single object which is an important difference between OOP and other paradigms.
What is a Class?
Classes are used for creating objects. We can say that a class is a blueprint or prototype for an object.
//name of class
public class Agent {
//Public Attributes
//Constructors
//Accessor Methods
//Public Interfaces
//Private Implementations
}
Let’s examine the class structure.
Class Names: One of the important things in class structure is its name. The name of the class should provide information about what does it do and why there is such a class.

In best practices, there are several rules about the naming convention of class. For example, don’t use verbs as a class name. Class names should have a noun or noun phrase. You must also consider programming language constraints.
Attributes: Attributes stores the information about the object. Each instance of the class will have its unique attributes and will have it is own copy in memory. So we can change attributes without affecting any other objects we’ve created. But if you want to create attribute that have only one copy in memory for all objects you can use static keyword.
Access Modifiers: With using access modifier keywords we can set the accessibility and restrict the scope of class, constructors, attributes and methods. The keywords and the meaning of these modifiers may differ from one language to another. For example, in Java, there are four types of access modifiers public, private, protected and default. We will learn about them later but for now, just know that when an attribute or method is defined as public, all objects can directly access it, when private, only that specific object can access it, when protected, related objects can access it.
Constructors: A constructors is a special methods that is used when an object of a class is created. Constructors are using for setting the newly created object to its initial, safe state. You can use more than one constructors in class. If you do not include a constructor, the system will provide a default constructor for you. Default constructor calls constructor of it’s superclass. By default the superclass will be part of the programming language. For example in Java Object class is superclass of all the classes by default.
Methods: As you can see in class definition I divided the methods in three part. Accessor Methods, Interface Methods and Private Methods. Let’s examine these methods respectively.
-Accessor Methods: As previously mentioned we can set accessibility of attributes with help of access modifiers. So if we use the private keyword while defining attributes this means other objects cannot access the attributes directly. Sometimes we need objects to interact with other objects but it does not need to do it directly. We can give access with help of the accessor methods also referred to as getters and setters.
-Interface Methods: You need to keep the public interface to a minimum as possible. Users of this object do not need to know about its internal workings. The public interface should contain only what the user needs to know. Have you ever look into which sorting algorithm your favorite programming language uses while sorting an array? As a software engineer maybe you should :) but as a user, you don’t need to as long as it does the job right.
-Private Methods: Private methods are the implementation of the class. You should hide the implementation from the user. In a good design practice changing the implementation of the class should not affect the user. For example, you are using some methods from the package which another team is developed. And this team decided to do some optimization and refactoring to this package. If they change one of the public interface methods users also should make these changes and if you are not aware of these changes your program will crash.
Let’s define an Agent class using the knowledge above.
In the example above we have two attributes for the Agent class. They are all private so it means another object can’t directly access these attributes. I created accessor methods for other objects to communicate with Agent Object. So each attribute has its own getter and setter method. As you can see Agent class has two constructors it means that we can construct Agent object in two different ways. One of them initializing the default Agent Smith object. Another one is expecting initial values of attributes of Agent class. With using new keyword and constructor methods of class we can create a new object.
Agent agentSmith = new Agent()
Agent agentJones = new Agent(name="Jones", heightInCM=188)
Agent agentBrown = new Agent(name="Brown", heightInCM=177)

Agent class has a public interface method fight and private implementation of the fight. Users have information that agents can fight and can use this behavior but the implementation of the fight will be hidden from users.
In this post, we talked about two of basic OO concepts: Object and Class. We know what is an object and how to define an object. We talked about class and class structure. In the next post, we will talk about Inheritance and give an answer to these questions What is Inheritance? and Why Inheritance is bad!?
Feel free for giving any suggestions and let me know if there is false information.

References
[1]: Matt Weisfeld. (May 2019). The Object-Oriented Thought Process, 5th Edition
[2]: Robert C. Martin. (August 2008). Clean Code: A Handbook of Agile Software Craftsmanship