© 2018 Anshul

- Anshul Kashyap; July 31, 2018
Before the creation of OOP (Object Oriented Programming) languages, some programs and algorithms such as varied data structure and typed programs were really difficult to implement. The first OOP language was published in 1985 by Bjarne Stroustrup and went by the name of C++. This revolutionized the programming industry and allowed programmers to create effecint and easy to understand programs with low compiling times.
Wait... but what is an OOP language? An Object Oriented Programming language is a programming paradigram based on the ideas of having Classes and Objects to represent data types and the data itself. Let me give you an example. Say you create a Person Class. This class can have multiple attributes that any person object has in common such as... eye color, height, weight, etc. When you create a Person Object, you are storing some data that represents the specific attributes of that specific object given the Person datatype. Think of the class as a blueprint and the object as the actual "thing". You can have a blueprint for a generic house, but you get to decide the house's wall color, flooring, etc.
/*In Java, when creating an object, you have to state the data type (Class), then the name of the var/object, and finally set the initial value of the object*/
int b = 0;
'''In Python, when creating an object, you have to state the name of the var/object, then set the value of that object. The data type (Class) is automatically initialized'''
b = 0
/*The initial values don't have to be set when creating the object in java. But beware of a NullPointerException*/
There are four basic principles that you have to know when it comes to OOP. They are Encapsulation, Inheritance, Abstraction, and Polymorphism.
public class Individual {
private int[] gene;
public Individual(int[] gene) {
this.gene = gene;
}
public int[] getGene() {return gene;}
public int calcFitness(int[] target) {
int fitness = 0;
for(int i = 0; i < gene.length; i++) {
fitness = fitness + (255 - Math.abs(gene[i] - target[i]));
}
fitness = fitness / 3;
return fitness;
}
}
This is a simple class I created when I was working on a generic Genetic Algorithm. Here you can see that the gene object is not accessible outside of the class. In java, when you declare data types, you also have to specify the availability of that object. If it's public, it can be accessed outside of the class. But if it's private, it can only be accessed inside of the class. You can see that this class is incorporating the idea of encapsulation which allows the programmer to work with a simple and clean interface.
public class Individual {
private int[] gene;
public Individual(int[] gene) {
this.gene = gene;
}
public int[] getGene() {return gene;}
public int calcFitness(int[] target) {
int fitness = 0;
for(int i = 0; i < gene.length; i++) {
fitness = fitness + (255 - Math.abs(gene[i] - target[i]));
}
fitness = fitness / 3;
return fitness;
}
}
public class Anshul extends Individual{
public Anshul(int[] gene, int skillLevel){
super(int[] gene);
skillLevel = 1000000000;
}
}
Here you see two classes, one the original Individual class and a new class named Anshul. We can see multiple keywords signifying that the idea of inheritance is being used. When creating the Anshul class, observe the key words extends and super. This shows that the Anshul class is inheriting the attributes stated in the Indiviudal class and setting their values in the constructor.
public class Individual {
private int[] gene;
public Individual(int[] gene) {
this.gene = gene;
}
public int[] getGene() {return gene;}
public int calcFitness(int[] target) {
int fitness = 0;
for(int i = 0; i < gene.length; i++) {
fitness = fitness + (255 - Math.abs(gene[i] - target[i]));
}
fitness = fitness / 3;
return fitness;
}
}
Take a look at the fitness variable in the calcFitness() function. We can see that fitness is local within calcFitness() and not global. This means that fitness is immutable outside of calcFitness(). This shows that some vars and objects in the program are actually hidden from the user, meaning it is not accessible outside the given function or class.
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
Say you have 4 classes: Object, Deer, Animal, and Vegetarian. Deer extends Vegetarian, Vegetarian extends Animal and all 3 classes initially extend Object. Here we can see polymorphism take place since the Deer object can take many different forms including Animal, Vegetarian, and Object. This shows that in OOP languages, we are able to create different classes that inherit attributes from each other. This allows us to create objects based on existing objects that don't have the same classes.