0% completed
Object-Oriented Basics
On This Page
Object and class
The four principles
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
How the four fit together
Key Takeaways
Object oriented programming is a way of writing software. Data and the code that changes that data live together, inside one thing called an object.
Procedural programming keeps them apart. There is a block of data, and there are functions that reach into it. Object oriented programming puts a wall around the data. Only that object's own methods get through the wall.
That single idea is what the interview is testing. It does not ask you to recite definitions. It asks you to split a problem into objects, decide what each one owns, and defend the split.
Object and class
Two words come first, because everything else is built on them.
A class is the description. It says what fields a thing has and what it can do. It is written once.
An object is one filled-in copy of that description, with real values in the fields. It is created many times.
An Account class describes what every account has. S-1, owned by Ada, holding 50,000 cents, is one object of that class.
In an online shopping system, Customer is a class. The customer who placed order 4192 last Tuesday is an object.
The four principles
Every example below uses one small banking design. Each principle adds to the same code instead of starting a new example.
The code appears in Python, Java and C++. All three print the same output, so read only the one you would use.
1. Encapsulation
Encapsulation means an object keeps its data private and lets other objects change it only through methods it controls.
The data is hidden. The methods are the only way in. So any rule about that data is written once, inside those methods, instead of being copied into every caller.
The box on the left has nothing protecting it. It is not in the code below. It is there to show what the closed version is being compared against.
Any code anywhere can set balance_cents to a negative number. When the rule changes, there is no single place to fix it.
The box on the right is closed. balance_cents is private, and deposit and withdraw both check the amount before touching it.
Notice what the two checks prevent. A caller cannot withdraw more money than the account holds. There is no way to reach the balance except through a method that refuses.
The class also carries monthly_update, which does nothing here. Section 4 is where it does something.
In the interview: when you write a field, ask who is allowed to change it. If the answer is "only this class", say so. The interviewer then hears a design decision rather than a list of fields.
2. Abstraction
Abstraction means showing only the operations a caller needs, and hiding everything about how they work.
Encapsulation hides the data. Abstraction hides the decisions. A caller sees a short list of operations. It stays correct even when everything behind the list is rewritten.
Here is a Bank that moves money between two accounts.
Read transfer again and notice what is missing. Account offers seven public operations, and Bank calls three of them: get_account_id, withdraw and deposit.
Bank does not know that a balance is an integer number of cents. It does not know which rules an account applies. It does not know that two different kinds of account exist at all.
That is why a balance could move from an integer to a database row tomorrow. transfer would not change.
In the interview: the phrase to use is "the caller does not need to know that". Every time you can say it truthfully, the design has become simpler.
3. Inheritance
Inheritance means a new class starts with everything an existing class has, then adds or replaces a part of it.
The new class is the child, and the existing one is the parent. The child inherits the parent's fields and methods without copying them.
A savings account pays interest. A checking account charges a monthly fee, unless the balance is high enough. Everything else about them is identical, so everything else is inherited.
Savings adds one field, checking adds two, and each replaces exactly one method. Neither one repeats the balance, the deposit rule or the withdrawal rule.
Inheritance is also the most misused of the four. The test is one sentence: "a savings account is an account". If that sentence is false, you want a field, not a parent.
A car is not an engine. So Car holds an Engine instead of extending it.
In the interview: if a child overrides a method to do nothing, or to raise an error, the parent was the wrong choice. That is a Liskov substitution failure: the child cannot stand in for its parent. Chapter 3 covers it.
4. Polymorphism
Polymorphism means one call can produce different behavior depending on which object receives it.
The word comes from Greek and means "many forms". In code, the caller holds a parent type and calls a method by name. The child decides what actually happens.
That loop is the reason the design is built this way. It never asks what type of account it is holding. It does not contain a single if about account types. Adding a fourth account type next month changes nothing in it.
Here is the program running from start to finish.
The output:
Gurus Bank, opening balances:
S-1 (Ada): $500.00
C-1 (Kai): $80.00
P-1 (Ravi): $30.00
moved $100.00 from S-1 to C-1
S-1 rejected, not enough money
month end:
S-1 (Ada): interest $1.00, balance $401.00
C-1 (Kai): fee $2.50, balance $177.50
P-1 (Ravi): no change, balance $30.00
Three accounts, one method name, three different results, and no branch anywhere that asks which is which.
In the interview: a long if or switch over a type is almost always a place where polymorphism belongs. Naming that out loud is worth more than the rewrite itself.
How the four fit together
Encapsulation and abstraction protect a class. They decide what stays inside and what is visible from outside.
Inheritance and polymorphism let a design grow. They decide how a new requirement arrives as a new class rather than as a change to an old one.
A design that uses all four looks the same every time. Private data, a short list of public operations, a parent that holds what is shared, and children that each replace one method.
Key Takeaways
- A class is the description; an object is one filled-in copy of it.
- Encapsulation keeps data private and puts every rule about that data in one place.
- Abstraction shows a caller a short list of operations and hides every decision behind them.
- Inheritance gives a child everything the parent has. It is only correct when "the child is a parent" is a true sentence.
- Polymorphism lets one call produce different behavior, which is what removes long branches on type.
- The interview does not ask for the definitions. It asks you to split a problem into objects and defend the split.
These four ideas are what a design is made of. The next lesson covers the process that produces one. It turns a description of a system into the objects inside it, one step at a time.
Abdallah Aqtash
· 3 years ago
You said that OOP organizes the program to combine data and functionality and wrap it inside something called an "Object", I think inside clas, yes or no?
Reading Progress
0%
On This Page
Object and class
The four principles
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
How the four fit together
Key Takeaways