Last Updated:

Object-Oriented Programming Concepts - OOP in Java

Understanding the basic concepts of object-oriented programming (OOP) is very important for the development of a novice Java developer, so in this article I will briefly go through each point and try to explain each of the principles in the simplest possible language.

Basic Principles of OOP


And now more about each:

Abstraction

Abstraction is the concealment of internal details and the description of things in simple words. For example, a method adds two integers, its internal logic is hidden from the outside world: it simply takes two numbers as input and outputs the result of addition. There are many ways to achieve abstraction in object-oriented programming, such as encapsulation and inheritance.

Any Java program is already a great example of abstraction: Java itself takes care of converting commands to machine language and hides the details of the implementation from the outside world.

Encapsulation

Encapsulation is a method of achieving abstraction in object-oriented programming. Encapsulation is used to restrict access to class members and methods.

One way to achieve encapsulation in OOP is through access modifiers. For more information about access modifiers in Java and how they affect encapsulation, see the link.

Polymorphism

In simple words, polymorphism allows an object to behave differently in different situations. There are two types of polymorphism: compilation and execution polymorphism.

Polymorphism in the compilation process is achieved by overloading the method. See an example below:

Here we have several conditional printing methods that have different behaviors. The names of the methods are the same, but the arguments of the methods (method signature) are different—this is what is called method overloading. The compiler must determine the method of the call at compile time, which is why this is called a compile-time polymorphism.

Run-time polymorphism is achieved when we have "IS-A" relationships between objects. The subclass must override the superclass method to achieve run-time polymorphism. If we look in the context of a superclass, the actual implementation of the class is determined at run time. The compiler is unable to decide which method of the class will be called. This decision will be made during the execution of the program, hence the name: runtime polymorphism.

Let's see an example:

 

 

We have 1 interface and 2 classes that implement it, now let's look at the runtime polymorphism:

In this Java example, the compiler does not know the exact class that implements the interface. This will all be determined at run time, hence this is an example of a run-time polymorphism.

Inheritance

Inheritance is one of the concepts of object-oriented programming, according to which one object is based on another object. Inheritance is a mechanism for reusing code. An object that is inherited is called a superclass. An object that inherits from a superclass is called a subclass.

Java uses a keyword to implement inheritance. Learn more about inheritance in Java. I also recommend the comparison article Composition vs Inheritance.extends

Association

Association is the concept of OOP, which defines the relationships between objects. The association also defines the multiplicity between objects, for example: the Object Teacher and the Objects Students. The relationship between the teacher and the students will be one to many. Similarly, a student may have one-to-many relationships with faculty objects. However, the student object and the teacher object are independent of each other.

Object-Oriented Programming

Aggregation

Aggregation is a special type of association. In aggregation, objects still have their own life cycle, but are limited by the "HAS-A" membership ratio, that is, the part-whole relationship between two objects.

Composition

Composition is a special, more rigorous form of aggregation. This means that an object in a HAS-A relationship cannot exist on its own. For example, the relationship "House-Room" - here the Room can not exist without the House. Read more about composition in this article.

This concludes a brief overview of OOP concepts. Have I missed something important in trying to make the article easy for a newbie to understand? If anything, write in the comments.