Sage-Code Laboratory
index<--

OOP Paradigm

A programming paradigm in Computer Science is a programming style. It consist of concepts and ideas that can lead to a better approach to create applications of quality. One of this paradigms is OOP = Object Oriente Programming that is addopted by Java à la carte.

Core Principles

Object oriented programming has four principles, called the pillars of OOP programming.

  1. Abstraction: you can define abstract classes and interfaces,
  2. Encapsulation: you can define public and private properties and members,
  3. Inheritance: one class can inherit properties and methods from parent class,
  4. Polymorphism: one method from a parent class can be overwritten in a child class.

Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (properties and attributes) and code acting on the data (methods) together as a single unit. Declare the variables of a class as private makes encapsulation stronger.

Inheritance

In Java you can define one class based on other class. This mechanism is called Inheritance. All classes are derived from a primary class called the Object class. So in other words a Class can "extend" another class. ;

In practice sometimes this model do not work. So we need to define other concepts: "mixin" and "dependency injection" that are advanced topics for future articles.

Polymorphism

A derived class "inherit" all properties and methods from base class. Sometimes we can "overwrite" a method to change the default behavior of base class. This allow us to extend and modify one class to create a different class that have similar but not exact the same behavior.

Advanced Features

Java offer other feature related to OOP. These features enhance even farther the capability of programmers to create good code and to reuse code made by other developers. However, learning these features require additional effort. Let's define these features:

Abstraction

We can use "abstract" modifier to declare a special Class that can't be instantiated. This kind of class is usually used as a base class to create "extended" class using a pattern. The pattern implements some but not all the functionality. Some functionality is implemented in the extended class.

Generics

Generics are classes that are not very specific for a particular data type. For example a dynamic list has the same logic but can apply to different primitive types: List of integers, List of floats and List of Double. In this case we can use a generic library to create a specific List.

This mechanism was created to relax a little bit the strong typing paradigm. It reduces the required lines of code we need to write for a specific design pattern. For dynamic programming languages this is not necessary since a particular object or data member can have any data type.

Interfaces

Interfaces are "abstract" concepts that force a specific behavior. Interfaces are almost the same as "abstraction" except that one class can implement multiple interfaces but can extend only one abstract class. So that's the main difference.

An interface use to have all methods abstract in older Java versions. Now you can create methods that have a default implementation using an interface. However most developers are using abstract method declarations for interfaces.


Read next: Abstraction