Avoiding The Oops in Oop
Jan 05, 2023
Introduction
There is an awful lot that goes into writing good code that abides by proper Object-Oriented Programming principles. In this tutorial I will basically cover the importance and significance of writing quality code (though I feel that quality code is subjective) thereby avoding “Oops in Oop.”
Note: “Oops” is basically an exclamation made after a significant mistake or accident rather.
Getting Started
Before being able to follow this tutorial and get the best out of it, you have to meet up with the following perquisites.
- Java (Java Development Kit)
- Functioning IDE, Intelli J or Vscode works fine. You can use Netbeans.
If you do not have a Java Installation, kindly proceed to the fourth section.
Java: OOP’s Bastard God Son
What is Java? Java is a general purpose language developed by James Gosling. It fully supports the Object-Oriented Paradigm and runs on the Java Virtual Machine, where it is compiled to Bytecode, that enables it appear as a universal language that can run anywhere! Yes that’s right. Fortunately unlike languages like Swift and C# that are platform otherwise operating system dependent - Java runs on all operating systems conveniently. Java is used heavily by huge firms, enterprise organisations, educational and government facilities. Java was the primary choice of development on the Android platform, before the advent of Kotlin’s full adoption. Java is stil used for building the server infrastructure for applications, games, mobile applications and so much more. Java’s JVM serves as a platform to compile Java programs into bytecode. Not only Java, but the JVM is so powerful that it is used to compile other languages such as Scala and Kotlin which I refer toas Java’s cousins.
Diagram of the Java-Virtual-Machine:
Java comes bundled in a package known as the JDK. The JDK is the Java Development Kit. It comes with the right tools for developing in Java. Not the IDE, but components such as the language itself, the Java Virtual Machine and Java Runtime environment. There are about three Java Editions offered by Oracle. They include: The Standard Edition, Enterprise Edition and Micro Edition.
But in this case, we would be using the Java Standard Edition.
Installing Java
To install Java is dependent on the operating system you are on. For Windows, it’s as simple as getting the installer up and running on your machine, and you can download here: JDK for Windows Let’s use Java 19. Or of you are a terminal dude like myself use Choclatey.
If you’re on a MacOS, you can install by opening up your terminal and running:
brew install openjdk@19
That would get you the Open JDK installed using the Homebrew Package Manager. Homebrew is a package management system for Linux and Unix machines that you can use for free. I’m on Linux now, but I prefer to use the apt-install
package.
For debian Linux, which I am on, run on bash"
sudo apt install openjdk-19-jdk-headless
On windows, you may need to add
JAVA_HOME
to your environment variables to get it up and running.
To Verify your installation, run on Linux and Mac:
javac -version
For Windows, run:
java --version
If it prints out your current version for you, then my friend you are good, ready set and up to go.
Setting up your Environment
I usually advice people to use an enabling Integrated Development Environment for ease of development in opposition to the traditional editors such as Emacs, Vim, or even Sublime Text. Please use Visual Studio Code, Netbeans or Intelli J (which by many is the best Java Development IDE available in the market)
What Exactly Is OOP?
OOP is a programming paradigm, that is responsible for basically writing scalable, functional and organised code for software systems. OOP, known as Object-Oriented-Programming is a model of programming and software design that is conceptualised on the implementation of data types and objects as opposed to functions. For more on Object Oriented Programming, check here
I won’t really dive deep into OOP, but you get the context here.
Principles of OOP
OOP has several principles that govern its implementation in programming. They include:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Those are the four basic principles we will cover. We will leave out composition and aggregation for another day.
1. Encapsulation
Not to confuse with abstraction, is a means of data security to help protect your data stored in a class from unauthorised access. Anytime you come across methods such as Public, Private and Protected, these are methods of encapsulation to save guard your code.
2. Inheritance
This is one of my favourite concepts of OOP. Inheritance is a means of referencing classes in code, i.e Parent to Child, otherwise Superclasses (Parent Classes) to Base Classes. By employing the extend
keyword each time, you tell Java to implement inheritance. Inheritance pretty much solves the DRY Problem which is “Do not Repeat Yourself.”
3. Polymorphism
Polymorphism is how a program can execute an action in different ways. Polymorphism has two forms in the Java Programming language, which includes method overloading (when various functions with the same name are within a class) and method overriding (when a child class overrides the parent method)
4. Abstraction
This is a process used to hide the overall complexity from users and present information that is relevant. In Java you can hide the details of implementation using interfaces or abstract classes. Abstraction also helps present the important piece of functionality.
Putting in Practice
Let’s write some code to implement the above principles of Object Oriented Programming.
1. Encapsulation/Data Hiding
You may have seen this example before. To find the area of a rectangle.
// Create parent class to accept fields.
class Area {
int breadth;
int length;
// Initialize Values
Area(int length, int breadth) {
this.length = length;
this.breadth = breadth;
}
// Function to calculate the Area
public void CalcArea() {
int area = length * breadth;
system.out.println("Area: " + area);
}
}
class Main {
public static void man(string[] args) {
Area rectangle = new Area(2. 16);
rectangle.calcArea():
}
}
This example above is to calculate the area of a rectangle. So included the two fields otherwise variables inside a single class which is accessible from other classes.