Java and Object Oriented Programming

Darrick Pang
4 min readApr 6, 2021

One of the first things I learned when it comes to a programming language is functions. In Java, a function, sometimes called a method, is written as

public class Main {
public static void example() {
// insert code here
}
}.

All functions must be written inside of a class in Java. On the other hand, Ruby is different because functions do not need to be in a class. The four pillars in Java OOP are abstraction, encapsulation, inheritance, and polymorphism.

  1. Abstraction is to use simple things like classes or objects to represent complex code.
  2. Encapsulation is to hide sensitive data from other people. For example, we want the bank to keep our sensitive information such as our bank number safe. To do that, such data must be declared “private”.
  3. Inheritance, like inheriting your parents’ estate, is merely inheriting attributes or functions from another class.
  4. Polymorphism is using a function to perform different tasks. Through inheritance, we can take a function and object from another class, and polymorphism allows us to use it for different purposes.

Now, let’s look at this using code. Abstraction is simple because it is a class. Let’s look at encapsulation. Here, we want to hide sensitive data from other people. To do that, we

public class BankNum {
private String number; // this makes it private so no access

// Getter
public String getNum() {
return number;
}

// Setter
public void setNumber(String newNumber) {
this.number = newNumber;
}
}

For Ruby and Rails, we also have encapsulation. To do that, we add private like so.

class UsersController < ApplicationController
...
private def user_params
params.permit(:name, :password)
end
end

If I were to call user_params, I would get an error. Also, labeling a function as private can also tell developers that the functions are needed elsewhere. For example, I usually build apps that use Rails and React and if I want to create a new user or log in, the user_params function is needed. I input the information through the front end using React and then send the information to the database through my private method. This is merely an explanation in a nutshell, and I have went off on a tangent. Let’s return to OOP pillars.

For inheritance, we want to grab the function from another class by using the term “extends”. This is from the W3 website.

class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {

// Create a myCar object
Car myCar = new Car();

// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();

// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}

We can see that when we run it, we get

Tuut, tuut!
Ford Mustang.

We can inherit a class in Ruby too. To do that in Ruby, we do

class Car < Vehicle
end

which we see that the symbol < is inheriting the Car class from the Vehicle class

class Vehicle
end.

Last one is polymorphism. We want to grab functions from other classes and do what we want with them. An example is from the W3 website.

class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}

class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}

This prints

The animal makes a sound
The pig says: wee wee
The dog says: bow wow.

We can see that inheritance and polymorphism are similar, but they are not. From the inheritance portion, we see the myCar object is calling the car brand from the parent class Vehicle; for polymorphism, we are grabbing those methods for different tasks. We create an object and then we call them in the Main class.

So far, this is what I have dived into regarding Java OOP. I hope to write another article on Java soon.

References

  1. Learn.co
  2. http://rubylearning.com/satishtalim/ruby_inheritance.html
  3. https://www.w3schools.com/java/
  4. https://stackify.com/oops-concepts-in-java/#:~:text=OOP%20concepts%20in%20Java%20are,to%20understanding%20how%20Java%20works.

--

--