There are three major components of Object Oriented Languages. To be truly Object Oriented, you must have all three of these components to make up your language.
Encapsulation
The protection of data is a very important aspect of object oriented programming. Encapsulation is the idea that we protect and contain the data for our object. Not only can we protect data, we can limit access to the methods as well.
Let’s look at an example. Say you have a multiplayer game. Each player has a screen name, score, and level. You wouldn’t want to edit one player’s level and accidentally change both players’ game level. Instead, each player’s data is separate and protected from the other.
This is the idea of encapsulation. You are ensuring that data from one part of a program, cannot accidentally influence another part of the program. Data in an object is not accessible by another object, without express consent, and never without directly calling that second object.
Secondary Advantage
We get a secondary advantage from encapsulation. That is since others developers don’t know how the data is stored, if we find a better way, we can update the class to work better, but not make the outside developers change their code since we can still use the same existing method names.
This is the idea of refactoring your code. You can make changes to it, hopefully improving it for speed, accuracy, and/or amount of data it can process, all while keeping those changes transparent to others who use the code you’ve written.
Levels of Access
Now different languages will have different levels of access available to you. As a minimum you will have public (anyone can see) and private (only your current object can see).
Java has two more levels: Protected and Default, sometimes called package.
With Protected your current class can see your object’s data. But so can a class derived from you, often referred to as a child class. This way child classes don’t have to use the methods to access the property, but outside classes still do.
The default, or Package level, of access allows any class within the package to be able to access that property or method.
Inheritance
Inheritance is the idea that you can have a class, which is based off of another class. When a class inherits from a base class, it gets a copy of all of the existing properties and methods. This provides a very fast way to create several classes which have related, or common fields, and methods.
Let’s consider a school application. Students, teachers, and administrators all share a lot of common data (name, home address, email, login, date of birth, etc.). This could be put into a common class that could be inherited by other classes, speeding up the development time.
Some languages have the notion of a class that is specifically designed to be inherited and not used directly. As such, they will not let you create an object from them. This is often called an abstract class.
A high level graphical representation might look like the following:
public abstract class Person {
private string name;
private date dob;
private Address home; // note that this is a class itself
// ... many other fields as necessary which all other classes would share
}
public class Student extends Person {
// gets all of Person’s variables and methods
private date expectedGraduation;
private string gradeLevel;
}
public class Professor extends Person {
// gets all of Person’s variables and methods
private string Department;
private string classesTaught[];
}
public class Administrator extends Person {
// gets all of Person’s variables and methods
// doesn’t have to add any variables or methods if they are unnecessary
}
Of course, if a Person’s method doesn’t function as it might need to for another class, that class can override it. That means that for the child class, they will use the method in their code, regardless of what the base class method is.
Multiple Inheritance
In some languages (C++ for example) you can inherit from multiple classes. This can cause some confusion if two classes have the same method name, or the same variable names. If there are two written out methods, which one is used?
To simplify, some languages (Java for example) will only let you inherit from a single class.
Interface
Sometimes there is a real need to have methods and/or properties from two different classes. For example, in Java, every class extends the base Object class. However, it doesn’t have every possible necessary method as that would take too much memory and resources. But Java won’t let you extend two classes.
Java’s solution to the multiple inheritance issue is Interfaces. An interface is a method, or collection of methods, which are imported into the class. In an Interface all of the methods are abstract, so they will need to be overwritten.
There are many common examples of using interfaces. However, a popular one is Seriable. If a class is seriable, it has a set of methods which allow you to serialize the data so it can be saved to a file, transmitted over a network, etc. How it needs to be serialized, and unserialized can be unique. And that is why that interface exists.
Polymorphism
Polymorphism allows for the same name in a method to be used multiple times, usually via function overloading, although there are other ways to use polymorphism, some are language specific, so we’ll just look at function overloading as polymorphism.
Let’s say you have a getAge() method. It does some basic math based upon the date of birth for the object compared to today’s date. However, you might want to know what age they’ll be at a given point in time. For example, you have to be 18 to vote in the US. However, you can register to vote before you turn 18, if you turn 18 before the next election. So you might want to see if a person is going to be 18 on a given date. In that case, your method might look like getAge(date futureDate).
The Three Components of Object Oriented Languages was originally found on Access 2 Learn