📄️ What Is Object-Oriented Programming?
Object-Oriented Programming, commonly referred to as OOP, is a fundamental programming paradigm that is based on the concept of “objects”. So you can think of all of the entities in your program as objects that have properties and can do stuff. Your code is structured to model real-word entities and their interactions within your program. We use something called a class to model these entities or objects. A class is basically a blueprint.
📄️ Creating a Class
In this lesson, we will create a class, which remember, is basically a blueprint for creating objects. We will then create an object from that class and access its properties and methods. The process of creating an object from a class is called instantiation.
📄️ Access Modifiers & Getters & Setters
In the last lesson, we learned how to create a class with properties, methods and a constructor. In this lesson, we will learn about visibility and access modifiers. Let's look at the class that we created in the last lesson:
📄️ Inheritance
In the last lesson, we looked at visibility and the access modifiers. I showed you an example of public and private, but I couldn't fully explain protected because we hadn't learned about inheritance yet.
📄️ Static Members & Methods
So far, we've only seen instance methods and instance variables. These are methods and variables that belong to an instance of a class. For example, if we have a User class, we can create many new instances of that user. However, there are cases where we don't need multiple instances and we want to create a method or variable that belongs to the class itself, not to an instance of the class. These are called static methods and static variables.
📄️ OOP Challenges
Now let's do a few simple challenges to practice what we've learned so far.
📄️ Abstract Classes
An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature; they cannot define the implementation.
📄️ Interfaces
Interfaces are similar to abstract classes, but they are not classes. They are a way to define a contract for a class to follow without having to define the implementation. An interface is a collection of abstract methods and constants. A class that implements an interface must implement all the methods and constants defined in the interface. An interface cannot be instantiated. It can only be implemented by a class.