Object, method, property
Let's take a closer look into each one of these concepts.
As the name object-oriented suggests, objects are quite important. An object is a representation of a "thing" (someone or something), and this representation is expressed with the help of a programming language. The thing can be anything—a real-life object, or some more convoluted concept. Taking a common object like a cat for example, you can see that it has certain characteristics (color, name, weight) and can perform some actions (meow, sleep, hide, escape). The characteristics of the object are called properties in OOP and the actions are called methods.
There is also an analogy with the spoken language:
Objects are most often named using nouns (book, person)
Methods are verbs (read, run)
Values of the properties are adjectives
Let's take, for example, the sentence "The black cat sleeps on my head". "The cat" (a noun) is the object ,"black" (adjective) is the value of the
color property, and "sleep" (a verb) is an action, or a method in OOP. For the sake of the analogy, we can go a step further and say that "on my head" specifies something about the action "sleep", so it's acting as a parameter passed to the
In real life, similar objects can be grouped based on some criteria. A hummingbird and an eagle are both birds, so they can be classified as belonging to the Birds class. In OOP, a class is a blueprint, or recipe for an object. Another name for "object" is "instance", so we say that the eagle is an instance of the Birds class. You can create different objects using the same class, because a class is just a template, while the objects are concrete instances, based on the template.
Data (stored in properties) and
The means to do something with the data (using methods)
One other term that goes together with encapsulation is information hiding. This is a rather broad term and can mean different things, but let's see what people usually mean when they use it in the context of OOP.
Combining several objects into a new one is known as aggregation or composition. The aggregation concept illustrates the ability to combine several objects into a new one. Aggregation is a powerful way to separate a problem into smaller and more manageable parts (divide and conquer). When a problem scope is so complex that it's impossible to think about it at a detailed level in its entirety, you can separate the problem into several smaller areas, and possibly then separate each of these into even smaller chunks. This allows you to think about the problem on several levels of abstraction. A personal computer is a very complex object. You cannot think about all the things that need to happen when you start your computer. But you can abstract the problem saying that you need to initialize the objects it consists of: the Monitor object, the Mouse object, the Keyboard object, and so on. Then you can dive deeper into each of the sub-objects. This way you are composing complex objects by assembling reusable parts.
Inheritance is a very elegant way to reuse code that has already been written. For example, you can have a generic object Person, which has properties such as name and date of birth, and that implements the functionality walk, talk, sleep, eat. Then you figure out that you need an object Programmer. You could re-implement all the methods and properties that Person has, but it would be smarter to just say that Programmer inherits Person, and save yourself some work. The Programmer object only needs to implement more-specific functionality, such as the method "write code", while reusing all of the Person's functionality.
When an object inherits from another object, it usually adds new methods to the inherited ones, thus extending the old object. Often the following phrases can be used interchangeably: "B inherits from A" and "B extends A". Also, the object that inherited a number of methods, can pick one or more methods and redefine them, customizing them for its own needs. This way the interface stays the same, the method name is the same, but when called on the new object, the method behaves differently. This way of redefining how an inherited method works is known as overriding.
In the example above, we had a Programmer object that inherited all of the methods of the parent Person object. This means that both objects provide a "talk" method, among others. Now imagine that somewhere in our code, there's a variable called Bob and it so happens that we don't know if Bob is a Person, or a Programmer object. We can still call the "talk" method on the Bob object and the code will work. This ability to call the same method on different objects and have each of them respond in their own way is called polymorphism.