Object-oriented programming begins with a change in perspective. Instead of treating a problem as a sequence of steps, it treats the problem as something made of objects: concrete instances extracted from the situation, with state and behavior that can be reasoned about directly. The point is not to decorate code with classes, but to use objects as the unit of thought.

A class is the general name for a set of things of the same kind. If a thing in the real world is abstracted into an object, the class is the collective term for objects of that type: birds, poultry, people. The class is also the specification that object construction relies on. A bird has a pair of wings and can use those wings to fly; essentially all birds share the wing characteristic and the ability to fly, and in that respect they have the same type. The class captures that shared specification.

Encapsulation

Encapsulation puts an object's attributes and behavior together, using the class as the carrier. The class normally hides implementation details from clients. What the client interacts with is the class's outward behavior, not the internal machinery that produces it. This is the first discipline of object-oriented design: keep state and the operations on that state in one place, and expose only what callers need.

Inheritance

Classes do not stand alone. Relationships between classes are called associations. An association describes a general binary relationship between two classes: a department store class and a salesperson class form an association, and so do a student class and a teacher class. Inheritance, by contrast, uses what specific objects have in common. Within a class structure, a subclass inherits from the parent class, so shared attributes defined higher up do not need to be repeated lower down.

Polymorphism

Polymorphism applies a parent-class object to the characteristics of subclasses. A graphics class makes this concrete. Every shape has the ability to draw itself; that ability can be treated as a behavior belonging to the class. If subclass objects are all treated as instances of the parent class, then drawing a shape becomes a simple call to the parent graphic class's drawing method. The same call can draw any shape.

Polymorphism lets a program be written in a uniform style while handling many existing and related classes. The uniform style can be implemented by the parent class. Working through that parent-class style, subclasses can be instantiated as needed. Because the whole process depends only on the parent class's method, maintenance later means maintaining and adjusting the parent method. That reduces maintenance difficulty and saves time.

Classes in code

The following snippets show a class named xwfun expressed in C++, Java, and PHP.

# include

using namespace std;

class xwfun
{
cout \<\<”hallo world”\<\<endl;
}

int main()
{
xwfun.run();
xufun.stop();
}
public class xwfun{
public string name;
public string came;
public string name(){
setcame(“Java”);
setname(“world”);
return this.came\+””\+this.name;
}
}

public static void main(string\[\] args){
try{

| ``` 1 ``` | ``` System.out.println(new xwfun()); ``` |
| --- | --- |

}catch(MyException e){

| ``` 1 ``` | ``` Systemctl.out.println(e); ``` |
| --- | --- |

}
}
\<?php
Class xwfun{
public function _construct(){
echo “Hallo world”;
}
}