Last Updated:

OOP Concepts - Java

What is an object?

 

Java is a so-called object-oriented (OO) language with which you can engage in object-oriented programming (OOP). This style of programming is very different from procedural programming and may seem a bit strange to most programmers who have not encountered OOP. First of all, it is necessary to understand what an object is; it is on this notion that the PLO is based.

 

An object is an independent piece of code that knows about itself and can tell other objects about it if they ask a question that it understands. An object has members (variables) and methods that are questions that it can answer (even if they don't look like questions). The set of methods that an object knows how to respond to is its interface. Some methods are public, which means that another object can call (or invoke) them. This set of methods is known as the public interface.

 

When one object calls a method of another object, it is called sending a message. This phrase corresponds to OO terminology, but most often in the Java world, people say "Call this method" rather than "Send this message". In the next section, we'll look at a conceptual example that should clarify all of this.

Conceptual example of an object

 

Suppose we have the object Man. Each object Human has a name, age, nationality and gender. Every object Man knows how to speak and walk. One object may ask another about its age, or may tell another object to start (or finish) moving. In programming terms, you can create a Person object and assign some variables (such as name and age) to it. If you create a second Person object, it can ask the first person its age or tell it to start moving. It can do this by calling the methods of the first Person object. When we start writing code in Java, you'll see how the language implements the concept of an object.

Typically, the concept of an object remains the same in Java and other object-oriented programming languages, although they implement it differently. This concept is universal. For this reason, object-oriented programmers, regardless of the language they use, communicate differently than procedural programmers. Procedural programmers often talk about functions and modules. Object-oriented programmers talk about objects and often talk about them using personal pronouns. You can often hear one OO programmer say to another, "This Supervisor object tells the Employee object here, 'Give me your ID,' because it has to assign jobs to the Employee."

Procedural programmers may find this way of thinking strange, but it is natural for OO programmers. In their software world, everything is an object (with some exceptions in the Java language), and programs are the interaction (or conversation) of objects with each other.

Fundamental Principles of OOP

Naturally, the concept of an object is a critical concept for OOP, like the idea that objects communicate with each other through messages. But there are also three other fundamental principles that you need to understand.

You can memorize three fundamental principles of OOP with the acronym PIE:

  • Polymorphism
  • Inheritance (Inheritance)
  • Encapsulation

These are all unusual names, but these concepts are not difficult to understand. In the next few sections, we'll take a closer look at each of them in reverse order.

Encapsulation

 

Recall that an object is a self-contained entity that contains data elements and the actions it can perform on those items. This is the implementation of a principle known as the concealment of information. The idea is this. The object knows about itself. If another object wants to know information about another object, it should ask about it. In OOP terms, it must send a message to the object, for example, to request information about its age. In Java terms, it must call an object method that returns age.

Encapsulation guarantees the individuality of each object, and programs represent communication between objects. The Java programming language allows a programmer to break this principle, but this is almost always a bad idea.

Inheritance

 

At your birth, biologically speaking, you were a combination of your parents' DNA. You weren't an exact copy of either of them, but you were like both. OO uses the same principle for objects. Imagine the object Man again. Remember that each of these objects has a nationality. Not all of these objects have the same nationality, but aren't they similar? Of course! It's not Horses, or Chimpanzees, or Whales. This is the object of Man. All human objects have certain common features that distinguish them from other types of animals. But they're a little different from each other. Does a Child Look Like a Teenager? No. They move and speak differently. But the Child is definitely a Man.

In OOP terms, the Person and child are classes in the same hierarchy and (most likely) the Child inherits characteristics and behaviors from his parent. We say that a particular Child has the Type of Person, or that this Child is inherited from the object Human. It doesn't work in the opposite direction – a human is not necessarily a Child. Every Child object is an instance of the Child class, and when we create a Child object, we create an instance of it. Represent the class as a template for instances of that class. Typically, what an object can do depends on the type of object or, in other words, which class it is an instance of. Both Child and Adolescent are of the Human type, but one can work and the other cannot.

In Java terms, Human is the superclass of the Child and Adolescent classes, which are subclasses of the Human class. Another concept related to inheritance is abstraction. A person is at a higher level of abstraction than a Child or Teenager. Both are of the Human type, but are somewhat different. All Human objects have some common properties (for example, name and age). Can you create an instance of the Human class? No. You have either an instance of the Child class or the Teen class. Man, in Java terms, is an abstract class. You cannot have a direct instance of the Human class. Only a Child or Adolescent, who both have the Type of Person, are real. Abstract classes are beyond the scope of this guide, so we won't talk about them again.

Now, think about what the action of "talking" means to the Child object. We'll look at the point of this in the next section.

Polymorphism

 

Does a Child "speak" the same way as a Teenager? Of course not. The child makes noise, which is not always recognizable words that use adolescent objects. So when I create an instance of a Child object (the saying "create an instance of a Child" means the same thing – the word "object" is implied) and tell it to "speak", it can coo or gurgle. It is expected that the Teenager will be more clear.

In the hierarchy of humanity, we have the Class Man at the top of the hierarchy and the Child and Adolescent classes below, as subclasses. All objects a Person can speak, so objects A Child and a Teenager can too, but they do it in different ways. The child gurgles and makes the simplest sounds. A teenager utters words. Here's what polymorphism means: Objects do things differently.

What is (and what is not) the object-oriented nature of the Java language

 

As we'll see, the Java language allows you to create top-notch objects, but not everything in the language is an object. This is a bit different from some other object languages, such as Smalltalk. Smalltalk is a pure object-oriented language, i.e. everything in it is an object. The Java language is a mixture of objects and other entities that are not objects. It also allows one object to know the insides of another if you, as a programmer, implement such a possibility. This violates the principle of encapsulation.

However, the Java programming language also provides every programmer with the tools they need to follow all the OOP rules and create very good object-oriented code. But it takes some discipline. Language doesn't force you to do the right thing.

While many ardent proponents of the object-oriented approach rightly argue about whether Java is object-oriented or not, it's not really that important. The Java platform appeared to stay. Learn how to apply OOP with Java where possible, and leave the purity arguments to others. The Java language will allow you to write clear, relatively concise and manageable programs that are good enough for most professional situations.