OOP Tutorial
An introduction to OOP (Object Oriented Programming).
Previously you may have written your code procedurally, which is where all your code is written within a single file and is often made up of a single method/no apparent methods, in which code would be run top down (procedurally). Whereas in OOP, classes and models are used to create representations of real world environments/datastore formats.
OOP Key Points
- A class is like the template for a set of data
- Objects are things formed by the class
- Actions that the object can perform are called methods
- Attributes are fields or public variables of the object
Object variables are sometimes made private to prevent unauthorized code from modifying it and to allow for that variable name to be reused in other classes. We have two types of methods to modify private variables:
- Mutator Methods: Often written as
set(variable name), it is used to set the variable’s contents. - Accessor Methods: Often written as
get(variable name), it is used to access the value of a variable private to that class.
Creating a Class
To begin our OOP coding adventure we are going to create a class which can be done as follows:
public class ClassName {
}
We are then going to name this class “Person” and inside of this class we are going to include a private variable called name which will store the name of a person:
public class Person {
private String name;
}
Now we are going to include the class constructor, which is simply a method that has the same name as the class. It is used to initialize the class. Inside of it is where the default values of the class’s variables are set (if applicable). More than one constructor can be made but they must take different parameters so that Java can recognize which constructor you are referring to. This is referred to as overloading a constructor, since all constructors must have the same name. If a constructor is not made, Java will create a default constructor. In our case, our constructor will take the person’s name as a parameter and if no parameter is passed it will set a default name of our person to “Jack”.
public class Person {
private String name;
public Person() {
name = "Jack";
}
public Person(String n) {
name = n;
}
}
After that we can now think about what we want our class to do. Since we have chosen to make our name variable “name” private, but we want access to it from outside of our class, we will use the two types of methods previously discussed to allow us to set & get our “name” variable.
public class Person {
private String name;
public Person() {
name = "Jack";
}
public Person(String n) {
name = n;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
We can now add additional functionality to our class beyond being able to modify the “name” variable. In this case I will be writing a method to return a message.
public class Person {
private String name;
public Person() {
name = "Jack";
}
public Person(String n) {
name = n;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public String message() {
return name + " is a great person!";
}
}
Using the Class
Now that we are done working on our class we can save that in a file called “Person.java” and start a new file. Our new file will have a similar class constructor as our previous file but with a different name:
public class OOPExample {
}
But in this case instead of a constructor we are going to use the main method to allow this program to be run directly:
public class OOPExample {
public static void main(String[] args) {
}
}
And inside of that main method we are going to create our person class with the name “Bob”. In my case I am naming the object “p” but you can name it whatever you like.
public class OOPExample {
public static void main(String[] args) {
Person p = new Person("Bob");
}
}
Now I can access the methods within Person like so:
public class OOPExample {
public static void main(String[] args) {
Person p = new Person("Bob");
p.setName("Mike");
System.out.println(p.message());
}
}
Inheritance
But this is a very basic example. Let’s hypothetically say that you wish to add additional functionality to your class, by adding an additional variable or maybe a method. This can all be done with inheritance, which allows you to extend the class, creating a subclass (the class that was extended will be called the superclass). The subclass will not have to redefine any of the variables or methods again as they are inherited from the superclass. The subclass also has the ability to override any of the methods in the superclass, allowing you to create a class similar to another class without having to rewrite all the code required for that class. As demonstrated below:
public class Europeans extends Person {
}
Casting
This concept can be extended by understanding class casting. UpCasting and Downcasting is a way to travel through the class hierarchy. Upcasting is going up a class level to its superclass. This operation can never fail because once an object is created, anything above it must have already been initialized. On the other hand, downcasting has the potential to fail. If an object is trying to go down the hierarchy it can fail because more than one subclass may exist.
THANKS FOR READING