Object Oriented Programming in Java

A few years ago I made my first attempt to learn the Java language, but as soon as I heard there were things called “Polymorphism” and “Abstraction” 🙁 I got really scared and assumed this language was too hard for me to learn. I couldn´t have been more wrong. The names are scary but their meaning is quite simple to understand.

Here are the terms I will be explaining on this post :

 

Encapsulation

The concept of encapsulation consists on protecting your instance variables against manipulation outside the class. You don´t want other classes to directly access your fields, so you declare them private and create methods that will set and get their state.
Doing so will give you great flexibility to change your code without breaking the code of whoever is using the class, you can change the type of your variables and adapt the existing methods to work with the new type and it wont break any class that uses it because the methods are invoked just like they were before the new code.

On the following example you will notice that the variables are declared with the keyword “private”. This means that they are only accessible from inside this class, not from other classes that create instances from this one.

Example :

public class ExampleEncap{
 
   private String name; //notice that we declared the variable as "private"
   private int age; //notice that we declared the variable as "private"
 
   public int getAge(){ //this method will return the age variable
      return age;
   }
 
   public String getName(){ //this method will return the name variable
      return name;
   }
 
   public void setAge( int newAge){ //this will set the age variable
      age = newAge;
   }
 
   public void setName(String newName){ //this will set the name variable
      name = newName;
   }
 
}

 

Using the encapsulated classes is very simple after this, just call the methods and you are good to go.

public class TestDriveEncap{
 
   public static void main(String args[]){
      ExampleEncap encap = new ExampleEncap(); //create a reference and assign a new object from our class
      encap.setName("James"); //call the setName method to change the name
      encap.setAge(20); //call the setAge method to change the age
 
      System.out.print("Name : " + encap.getName()+" Age : "+ encap.getAge()); //output the name and age by calling their get methods
    }
}

Inheritance

Inheritance consists on making a class inherit the same properties of another, and can also implement new variables and methods while having the inherited ones at the same time. You can imagine an hierarchical tree with classes descending from other classes while keeping their inherited properties intact.

In Java, all objects descend from the “Object” class. When you create your new class, you are applying inheritance because  the compiler will automatically make your object descend from the “Object” class. When you create a new object you always have some methods that you did not create yourself, like “equals()” and “toString()” because this methods have been inherited from the “Object” class.

When developing the object model of a Java application you will have relationships between classes and there can be a “IS-A” relation and a “HAS-A” relation.

The “IS-A” relationship directly refers to inheritance. It´s like saying that a certain object is the same type as another object.

Imagine you have the classes “Computer”, “Laptop”, “Desktop”, “CPU” :

Computer HAS-A CPU

Laptop IS-A Computer

Desktop IS-A Computer

The “Laptop” and “Desktop” classes are sub-classes that inherit properties and methods from the super-class “Computer”, so your “Laptop” and “Desktop” classes also have a “CPU”. You can say that :

Laptop HAS-A CPU

Desktop HAS-A CPU

because their superclass have a CPU and by the laws of inheritance they also have it.

Inheritance can be achieved using the keywords “extends” and “implements“.

The “extend” keyword is applied to create a class that extends (inherits) another class.

Example :

 

public class Computer{
}
 
public class Laptop extends Computer{
}
 
public class Desktop extends Computer{
}

 

In this example we have created a superclass named “Computer” and two subclasses name “Laptop” and “Desktop” that derive from the superclass.

The “implement” keyword also applies inheritance on your class but this is used with interfaces and we will discuss this later in this article.

This document is a Work In Progress.

 

This entry was posted in Java, Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please calculate the arithmetic expression to validate you are human. *