Objects
- An object is similar to an abstract datatype:
- Contains some hidden data with a set of operations on them.
- All interactions with this hidden data is though certain operations β methods, member-functions,β¦
- Examples:
- A counter β an object which holds a single integer value.
- A filesystem or database β an object with large number of data with a number of operations on them.
Features of Object Oriented Programming:
- Abstraction.
- Subtyping.
- Dynamic Lookup.
- Inheritance.
Abstraction
<aside>
π‘ Main idea: Display only useful details and hiding unnecessary ones.
</aside>
- Objects are similar to abstract datatypes:
- They have a public interface visible to outside world.
- But their implementation is private an hidden from outside world or abstracted.
- Changing implementations should not affect the interactions with the object.
Subtyping
<aside>
π‘ Idea is to have a queue that can hold objects of different types. Subtyping is the way to achieve this.
</aside>
-
Solution is to arrange the type of the objects in a hierarchy.
- A subtype is a specialization of a type. (e.g. e-ticket is a subtype of ticketβ Has all features of a ticket and more)
<aside>
π‘ If A
is a subtype of B
, wherever an object of type B
is needed, an object of type A
can be used.
</aside>
- Think of it like this: Every object of type
A
is also an object of type B
- Or like subsets: If
$X\\sube Y$
, every $x \\in X$
is also in $Y$
.
If f()
is a method of B
and A
is a subtype of B
, every object of A
also supports f()
Note: Implementation of f()
can be different in A
.