Skip to content

Latest commit

 

History

History
84 lines (66 loc) · 1.99 KB

class-based-oop.md

File metadata and controls

84 lines (66 loc) · 1.99 KB

Class-based Object-oriented Programming

There are at least two OOP models in common use, class-based OOP (as used in Java and C++) and prototypal OOP (as used in Javascript). Some languages include features from both models. I have heard class-based OOP referred to as classical OOP but I think that is one of those language accidents you sometimes hear about.

Class-based OOP is centered around classes. A class is a specification for how an object should be constructed, what data comprises its state, and what operations can be applied.

Different languages might add additional parts to a class specification: static methods, for example, or whether a class can be extended through interitance.

The parts of a class common to most languages are constructors, data members and function members or methods.

Constructor

A constructor specifies how to create a new instance of a class. An instance is what people usually mean when they say object. A constructor is just a function and it might or might not take parameters. Many languages allow multiple constructor functions to be defined for one class.

class Food {
  // ...

  // constructor
  public Food(String name) {
    // initialize all the data for a new
    // instance of "Food."
  }

  // ...
}

Food bean = new Food("lima");

Data Member

The data members of a class specify what information will comprise an instance's state. These usually look like variable declarations inside of a class specifiation.

class vehicle {
private:
  // data members
  int mileage;
  date manufactured;

// ...
};

Function Member

The function members of a class define the operations its instances can perform. Typically, a function member operates on an instance's data members.

class Counter {
  // data members
  private int value;
  
  // function members
  public void increment() {
    value = value + 1;
  }
  public void decrement() {
    value = value - 1;
  }
  public int value() {
    return value;
  }
}