Principles of Object-Oriented Programming

Object-oriented programming is a programming paradigm that uses objects rather than logic and functions to implement a concept. Regardless of the programming language, there are four specific principles of object-oriented programming. The principles guide different aspects of programming including complexity, usability, reproducibility, and design of the program. The four principles of object-oriented programming include abstraction, inheritance, encapsulation, and polymorphism.

Abstraction hides the complexity of the implementation complexity to achieve generalization. Abstract class- is a template for a class where some of the functionality is not implemented yet. Abstract classes cannot be instantiated directly. Concrete classes can extend an abstract class and implement the appropriate functionality.

Encapsulation is a principle that guides the process of binding an object’s state and behaviors together. With encapsulation, we can hide a class’s data from other classes. We can achieve encapsulation in Java by declaring all the fields in a class as private and writing public methods in the class to set and get their values. We use encapsulation so that the user has no idea of the inner implementation of a given class and the data it contains. Encapsulation allows you to hide how values are stored and maintained within a given class. In addition, encapsulation hides unwanted implementation details from the user of an object, improves the reusability of our program, and makes our codes more maintainable. The other merit of encapsulation is that it makes unit testing much easier because each piece is less coupled. In Java, we can achieve encapsulation by declaring all the fields in a class as private and writing public methods in the class to set and get the values of variables. With this, other classes can still access this hidden data, but they can only do so indirectly through a public method of the given class.

Inheritance is a process where one class acquires the fields and methods of another through extensions. With inheritance, we can write common properties and functionality in one class and have other classes with unique features extend this single class. Instead of writing the same common implementation multiple times, we can write it once, and then whenever a class needs the functionality, extend that class. The main advantage of inheritance is minimizing the amount of duplicate code across multiple classes without writing additional code. To change behavior, you can override and modify the appropriate methods while keeping all the other behaviors. Inheritance is different from pure abstraction because both classes are concrete classes. On the contrary, the disadvantage of inheritance is that the superclass and the subclass can become tightly coupled, meaning they cannot be used independently of each other. The program has more levels of implementation to jump through in order to find the appropriate functionality.

Polymorphism is the ability of an object or function to take many forms. Depending on the content or the situation, the form may be different making your code more flexible and reducing complexity. For example, Java supports two forms of polymorphism; runtime polymorphism which uses the overriding method, and compile-time polymorphism which uses the overloading method.