This document discusses key concepts of object-oriented programming including:
- Objects have state (fields) and behavior (methods) like real-world objects.
- Classes are used to group similar objects and define common attributes and behaviors.
- Inheritance allows subclasses to inherit attributes and behaviors from parent classes.
- Polymorphism allows classes to share a common interface while having different implementations.
- Abstract classes define common functionality and interfaces for subclasses to implement.
2. Objects are key to understanding object-
oriented programming.
Objects are like real-world objects like cat or
car
Real-world objects share two characteristics:
They all have state and behavior.
For example cats have state (name, color,
breed, hungry) and behavior (meowing,
fetching, wagging tail, playing).
3. Same as real-world object, object in
programming also have that two
characteristics : state (fields) and behavior
(methods)
Hiding internal state and requiring all
interaction to be performed through an
object's methods is known as data
encapsulation — a fundamental principle of
object-oriented programming.
4. In the real world, you'll often find many individual
objects all of the same kind. There may be
thousands of other cats in existence, but all of
them have same state and behaviour.
That’s why we use class to classify them.
Objects of the same kind are said to be members
of the same class.
Let’s say there are Angora Cat, Persian Cat and
Common Cat. All of them are members of Cat
class and they have same basic state and
behaviour.
5. Furthermore, if we want to make the
classification bigger we can use Inheritance
Imagine a class with a very big scope let’s say
we have a class named Animal that have
fields legs, isHungry, isAlive, etc and methods
eat, sleep, etc
The attribute in the class Animal can be
inherited to their sub/child class
6. For example we’ve a class named cats that
extends to animal class
That means animal class is the parent/super
class of cats class
Cat class will get all the same fields and
methods as animal class
However, you must take care to properly
document the state and behavior that each
superclass defines, since that code will not
appear in the source file of each subclass.
7. This is a table of access member control that
define which member of the class can be
accessed
Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
8. Polymorphism describes a pattern in object
oriented programming in which classes have
different functionality while sharing a
common interface.
9. An interface is similar to a class except that it
cannot contain code. An interface can define
method names and arguments, but not the
contents of the methods.
Any classes implementing an interface must
implement all methods defined by the
interface.
13. Method overloading deals with the notion of
having two or more methods(functions) in the
same class with the same name but different
arguments.
While Method overriding means having two
methods with the same arguments, but
different implementation. One of them would
exist in the Parent class (Base Class) while
another will be in the derived class(Child
Class).
14. An abstract class is a mix between an
interface and a class. It can define
functionality as well as interface (in the form
of abstract methods). Classes extending an
abstract class must implement all of the
abstract methods defined in the abstract
class.
15. PHP :
abstract class AbstractExample {
public $name;
public function doThis() {
// do this
}
abstract public function doThat();
}