What is Object Oriented Programming (OOP)

Follow this tutorial to understand the principles of object oriented programming and write reusable and clean code

This article aims to explain the principles and concepts around OOP and the 4 major terms you need to know when we talk about OOP: encapsulation, inheritance, abstraction and polymorphism. Although those principles are in full power for every object-oriented programming languages like: Java, Python, C++, Ruby etc., I will provide the examples in Java. Why? Because, first of all this is a Java tutorial website and second Java is so object-oriented, that everything in Java is  an Object. Wait! Primitive types like int, double, long etc. are not objects you may say. Well, that’s true, but even primitive types have Object representation in Java. int is Integer, double is Double and so on.

Also be prepared to encounter object-oriented questions on your next Java job interview. They are so common, that there is nearly 100% chance you will be asked about one or more of the OOP principles.




What is OOP

Wikipedia defines OOP as a: “programming paradigm based on the concept of objects, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.”

Object-Oriented Programming (OOP) is a programming language model, which uses Objects and data as core components. If you want more details and better understanding of Classes and Objects, I have a detailed tutorial on this topic. Click here to read it first.

The main idea behind OOP is to represent the data and logic with objects instead of actions or functions. Think of objects as real-life objects… cars, buildings, animals, apples etc. And also abstract objects (things we can not see or eat) like HttpConnection or UserDataParser.  All of those have properties and methods to manipulate and access the data stored inside them. Eventually we can “convert” everything into an object.

So, what’s the big deal about having all this objects and OOP in general? Well, nobody can stop you writing your Java programs in one big file with hundreds of functions (except of your team lead or boss of course), but once you follow the principles of object-oriented programming you will produce reusable, maintainable, scalable and clean code.

Here are the 4 major Java OOP principles:

Encapsulation

Encapsulation is all about wrapping variables and methods in one single unit with the sole purpose of data hiding from external classes. Read the full Java Encapsulation Example for more details.

Inheritance

Inheritance is the OOP ability that allows Java classes to be derived from other classes. Read the full Java Inheritance Example for more details.

Abstraction

Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. Read the full Java Abstraction Example for more details.

Polymorphism

Polymorphism in Java allows subclasses of a class to define their own unique behaviours and yet share some of the same functionality of the parent class. Read the full Java Polymorphism Example for more details.

 

3 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments