Classes, instances, prototypes and inheritance in OOP

The idea of creating ​​object types is to use a mold or template. There are two different techniques as to how to use a mold. These techniques are; class-based programming and prototype-based programming.

Class-based programming

The class-based programming is the most widely used by the object oriented programming languages. Eg: Languages ​​Java, C + +, C #.

The concept is simple; there is a mold called class, where we specify the fields and methods we want our object to have. Whenever we need an object we create an instance (or copy of the object) using the class as a template.

Once the instance is created, we can modify the values ​​of its fields, giving it a unique personality.

For example, if the class is Dog and it has a field name; we can create a dog named Sultan and another one named Pufy.


Example of a class Dog in Java

  public class Dog {
	private String name; // I declare a field or instance variable inaccessible from outside the object 

	public void setName(String name) { // method to modify the name field from outside the object = name;

Example of how to use the Dog class to create new instances of objects:

    Dog dog1;     // I declare a variable type Dog, called dog1 
    Dog dog2; 

    dog1 = new Dog();       // I create a dog using new over the Dog class 
    dog1.setName("Sultán"); // I modify the name of the instance using the setName method
    dog2 = new Dog();


Imagine we want to work with the object dog and cat. Both are animals and have characteristics in common.

It may be the case, that when we begin to create the fields and methods for each one of them, we find duplicated code.

The idea of Inheritance, is to create a class called Animal, where we can write the duplicated methods and fields. When we create the classes Dog and Cat, we have to indicate that they inherit all the characteristic from the Animal class.




    public class Animal {
        protected String name;
        public void setName(String name) {
   = name;
    public class Cat extends Animal{ // The class Cat extends or inherits from the Animal class  
     ... Specific cat fields and methods ...
    public class Dog extends Animal{ 
     ... Specific dog fields and methods ...

Prototype-based programming

This style of object-oriented programming is also known as instance-based or classless programming. The mechanism for code reuse is given by the cloning of objects instead of using classes.

Let´s see our example of dogs and cats: We create an Animal object, we clone it and add fields and methods to create a generic Dog object. Each time we need a new dog, we clone the generic object Dog and we change the field name.

Even though there are few object-oriented languages ​​that use the prototype-based programming, amongst them we have Javascript, which could be the most important Internet language. Python and Ruby are other important languages ​​that support prototype-based programming.