four essential features of object Encapsulation Abstraction Inheritance Polymorphism

(爛尾了,哈哈)

There are four core concepts in object-oriented programming. They are encapsulation, abstraction, inheritance and polymorphism.

In OOP, program consists of a set of objects.

In the world of OOP, almost everything is an object.

Let’s look at the following example. Sorry for the ugly drawing.

This example is going to show the power of object-oriented programming. Let’s imagine that you are asked to make a car. Then, you will start to think what are the components of a car. Battery, axle, brakes, doors, windows, these are the examples of components. In object-oriented programming, we call these component objects.

Object

A object is an entity that contains data and methods.

Note: object can also be the data of other objects.

Every object contains data and methods.

Data

Information that stored in an object.

Method

actions that an object can do.

Note: There is difference between classes and instances. classes is the blueprints for new object, while instance is the object that is created from its blueprint(class).

Encapsulation

Hiding the implementation details of an object.

To understand the concept of encapsulation, recall the analogy of cars as objects.

Benefit of Encapsulation

  • to protect program data
  • clients can use the program without any understanding of the implementation details

Abstraction

Inheritance

Polymorphism