As a developer, if you spend enough time around Object Oriented Programming (OOP), you’ll begin to develop an intuition for various OOP principles and terms. However, it’s not always easy to authoritatively define what exactly those terms and principles mean.

I recently found myself in just such a situation, so I took the time to get down in writing some solid OOP definitions I can refer back to over time.

The following are adapted from Wikipedia, my own life experience, and, where noted, several random places around the internet. I hope the following is helpful either as an introduction or as a brief review of some important OOP concepts.

Object – A conception for modeling real world data consisting of state (stored in fields or variables) and behavior (methods or functions).

Class – The blueprint for creating an object. Defines what state and behavior an object should have.

Thus, an object is an instance of a particular class, much like a Dog is an instance of an (or particular type of) Animal.

There can be multiple relationships between classes, including the following (adapted from [here][soveflow-link]{:target=”_blank”}):

(Please Note: In computer programs, the relationships below are often discussed in the context of what happens to one object when another object goes out of scope, i.e., dies. Thus, the discussion of a dog’s relation to various objects both during and after its life. No animals were harmed in the writing of this blog post!)

Association: uses a
Ex: A [Class] Dog uses a [Class] Bone.

Aggregation: has a
Ex: A [Class] Dog has a [Class] Collar. (The Collar is still there when the Dog dies.)

Composition: owns a
Ex: A [Class] Dog owns a [Class] Heart. (When the Dog dies, the Heart dies.)

Inheritance: is a
Ex: A [Class] Dog is a [Class] Animal. (Dog is an Animal while living and when dead.)

Inheritance – The ability of a class to derive state and behavior from other classes.

Because the Dog class inherits from the Animal class, instances of the Dog class get state and behavior from the Animal class while also being free to specialize. For example, while the Animal class may have a generic makeNoise() method, the Dog class may implement a more specific bark() method that overrides the behavior of the animal makeNoise() method.

Polymorphism – An object of type B can appear and be used as an object of type A.

For example, assume that the Dog class is a subclass of (i.e., inherits from) the Animal class:

dog d = new dog();
animal a = d;
a.makeSound(); // Will call d.bark()

For an example of when this might be useful, think about being able to define a printer class and then being able to call print() without having to know the type of printer doing the actual printing. Since that aspect of the program can be abstracted away, it makes for more flexible, maintainable code!

Duck Typing – If an object implements the expected behavior, it can be treated like an instance of the class that defines that behavior. (In your code, if it walks like a duck and talks like a duck, it can be treated as if it were a duck! And the same holds true for dogs… ;)

Abstract Class – A class that can implement some or all of its methods but that cannot be instantiated. Instead, an abstract class must be subclassed in order to be used. An abstract class’s subclasses must extend the abstract class’s methods.

Since you won’t find an example of a generic Animal in real life, we will most likely implement Animal as an abstract class in our code. In order to use animal then, we’ll need to inherit from it, as in the case of the Dog class discussed above.

Interface – A description of the actions that an object can perform. In order to be an object that is an instance of class A, the object must implement the interface of A’s class.

Think about the above in light of the Animal/Dog class example above. If the Animal interface has methods like makeNoise(), eat(), etc., our Dog class must implement and/or override all of them in order to be a subclass of the Animal class.

I hope this post was helpful – please let me know your thoughts if you get a chance.