Hello and welcome! In this lesson, we'll dive into Object-Oriented Programming (OOP). Object-Oriented Programming is a coding paradigm inspired by the real world. It envisages everything as an "Object" with certain properties (characteristics) and capabilities (methods). Take a football, for instance: it has properties like size and color and methods such as being able to be kicked or caught.
In Scala, a Class provides a blueprint for creating objects. It is by using these classes that we instruct Scala on how an object should look and function. We can declare a Class using the class keyword.
Properties (characteristics) and methods (actions) that our object can possess and perform are also defined within the Class. Let's look at a simple Football Class:
An Object is an actual instance of a Class, crafted based on the blueprint we set in our classes. Creating an Object in Scala is straightforward; we use the new keyword followed by the class name:
Here, myFootball is an object of the Football Class, possessing the properties and methods we defined within the Football Class.
We're not limited to creating Objects; we can interact with them too by accessing their properties or invoking their methods using the dot operator ..
For example, to access the color property of the myFootball object, we write:
And to make use of the kick method of the myFootball object, we can use:
This gives us the ability to interact with digital objects in a way that closely mirrors how we interact with objects in the real world!
