From a98425810c53e0470fc2953f52c8bc3e5ec4879e Mon Sep 17 00:00:00 2001 From: Hayk Petrosyan Date: Mon, 19 Jul 2021 22:31:47 +0200 Subject: [PATCH 1/2] Classes and functions created --- README.md | 275 +++++++++++++----- 01-classes.php => exampels/01-classes.php | 0 .../02-properties.php | 0 03-methods.php => exampels/03-methods.php | 0 04-getters.php => exampels/04-getters.php | 0 05-setters.php => exampels/05-setters.php | 0 .../06-constructors.php | 0 .../07-inheritance-problem.php | 0 .../08-inheritance-solution.php | 0 .../09-public-private-protected.php | 0 10-static.php => exampels/10-static.php | 0 11-const.php => exampels/11-const.php | 0 .../12-abstract-classes.php | 0 .../13-interfaces.php | 0 .../14-overriding.php | 0 .../15-overloading.php | 0 .../16-namespaces.php | 0 mobileLibs.php => exampels/mobileLibs.php | 0 exercices/AbstractDevice.php | 45 +++ exercices/Device.php | 64 ++++ exercices/InterfaceDevice.php | 61 ++++ 21 files changed, 375 insertions(+), 70 deletions(-) rename 01-classes.php => exampels/01-classes.php (100%) rename 02-properties.php => exampels/02-properties.php (100%) rename 03-methods.php => exampels/03-methods.php (100%) rename 04-getters.php => exampels/04-getters.php (100%) rename 05-setters.php => exampels/05-setters.php (100%) rename 06-constructors.php => exampels/06-constructors.php (100%) rename 07-inheritance-problem.php => exampels/07-inheritance-problem.php (100%) rename 08-inheritance-solution.php => exampels/08-inheritance-solution.php (100%) rename 09-public-private-protected.php => exampels/09-public-private-protected.php (100%) rename 10-static.php => exampels/10-static.php (100%) rename 11-const.php => exampels/11-const.php (100%) rename 12-abstract-classes.php => exampels/12-abstract-classes.php (100%) rename 13-interfaces.php => exampels/13-interfaces.php (100%) rename 14-overriding.php => exampels/14-overriding.php (100%) rename 15-overloading.php => exampels/15-overloading.php (100%) rename 16-namespaces.php => exampels/16-namespaces.php (100%) rename mobileLibs.php => exampels/mobileLibs.php (100%) create mode 100644 exercices/AbstractDevice.php create mode 100644 exercices/Device.php create mode 100644 exercices/InterfaceDevice.php diff --git a/README.md b/README.md index e644e6a..a250c11 100644 --- a/README.md +++ b/README.md @@ -2,135 +2,270 @@ # Assembler School: OOP Basics with PHP -In this project you will learn the basics of OOP using mobile devices as a reference. In the _"Project files"_ section you will find a description of the content to be displayed in each file. +In this project we created PHP OOP logic with diferent types of Classes using mobile devices as a reference. -## Table of contents +In the _"exampels"_ folder you will find exampels of PHP OOP clases and functionality. +In the _"exercices"_ folder you will find the files with the Classes and functions. +
+
-- [Table of contents](#table-of-contents) -- [Getting Started](#getting-started) -- [Dependencies](#dependencies) -- [Tools](#tools) -- [OOP Introduction](#oop-introduction) -- [Project files](#project-files) +# Questions -## Getting Started +What is object-oriented programming in general terms? -### The repo +``` +Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. + +OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. +``` + +What is a class? + +``` +In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). + +The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code. +``` + +What is an object? + +``` + An Object is an individual instance of the data structure defined by a class. We define a class once and then make many objects that belong to it. Objects are also known as instances. +``` + +What is an instance? + +``` +An instance is a concrete occurrence of any object, existing usually during the runtime of a computer program. Formally, "instance" is synonymous with "object" as they are each a particular value (realization), and these may be called an instance object; "instance" emphasizes the distinct identity of the object. The creation of an instance is called instantiation. +``` + +What is a property? + +``` +Properties are as in everyday language and technically are fields of objects/classes with dedicated getter/setter routines (which can be considered as methods). +``` + +What is a method? + +``` +Methods ("member functions") are similar to functions, they belongs to classes or objects and usually expresses the verbs of the objects/class. For example, an object of type Window usually would have methods open and close which do corresponding operations to the object they belong. +``` + +What is the difference between a function and a method? + +``` +A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not. + +``` + +What is a constructor? + +``` +A constructor is a special method used to initialize objects. +we use constructors to initialize all variables in the class +when an object is created. As and when an object +is created it is initialized automatically passing the data to +the constructor parameters. +``` + +What is the difference between a class, an object and an instance? + +``` +The Class is a “template” / “blueprint” that is used to create objects. +Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). They consist of state and related behavior wiche is given by the Class. +An instance is a unique copy of a Class that representing an Object, wiche is given by the constructor of the class Object. When a new instance of a class is created, the proccesing engine will allocate a room of memory for that class instance. -First, you will need to clone the repo: +``` + +What do we understand about the concept of encapsulation? + +``` +Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them +``` + +What do we understand about the concept of abstraction? + +``` +Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. +``` + +What do we understand about the concept of inheritance? + +``` +it is used to define relationship between two class, +which a child class occurs all the properties and +behaviours of a parent class. +Provides code reusability. We can implement by using +extend keyword +``` + +What do we understand about the concept of polymorphism? + +``` +Polymorphism is the method in an object-oriented programming language that performs different things as per the object's class, which calls it. With Polymorphism, a message is sent to multiple class objects, and every object responds appropriately according to the properties of the class. +``` + +What do we understand about the concept of Overload? -```bash -$ git clone https://github.com/assembler-school/oop-basics.git +``` +AnswerOverloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. ``` -### Presentation material +What do we understand about the concept of Override? -- [Slides](https://docs.google.com/presentation/d/1cZxutGPDqUGsLWLVen_ATjd7dEkeoPS_v_fy1y0C5Co/edit?usp=sharing) +``` +Overriding is a feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes. +``` -## Dependencies +What differences exist between the concept of Overload and Override? -Before we can get started you will need to make sure that all the necessary dependencies are installed in your system. +``` +1. Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism. -### PHP +2. The method Overriding occurs between superclass and subclass. Overloading occurs between the methods in the same class. -You can install it by following the instructions [in the official docs](https://www.php.net/downloads) (we recommend that you install the version that is named _Current_). +3. Overriding methods have the same signature i.e. same name and method arguments. Overloaded method names are the same but the parameters are different. -To verify that you have installed it correctly, you can run the following command from the terminal that should output the version installed: +4. With Overloading, the method to call is determined at the compile-time. With overriding, the method call is determined at the runtime based on the object type. -```bash -$ php -version +5. If overriding breaks, it can cause serious issues in our program because the effect will be visible at runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix. ``` -## Tools +What is a static class? -In the event that you prefer to use a tool that installs everything you need to configure and run a PHP server, we recommend using [XAMPP](https://www.apachefriends.org/es/download.html) +``` +Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor. +``` + +Look for 3 advantages over object-oriented programming compared to other programming paradigms + +``` +1.Modularity for easier troubleshooting -### XAMPP +When working with object-oriented programming languages, you know exactly where to look. “Oh, the car object broke down? The problem must be in the Car class!” You don’t have to muck through anything else. -XAMPP is a completely free and easy to install Apache distribution that contains MariaDB, PHP, and Perl. The XAMPP installation package has been designed to be incredibly easy to install and use. +2.Reuse of code through inheritance -xampp homepage +Suppose that in addition to your Car object, one colleague needs a RaceCar object, and another needs a Limousine object. Everyone builds their objects separately but discover commonalities between them. In fact, each object is really just a different kind of Car. This is where the inheritance technique saves time: Create one generic class (Car), and then define the subclasses (RaceCar and Limousine) that are to inherit the generic class’s traits. -You have to go to the [download page](https://www.apachefriends.org/es/download.html) and it will automatically recommend installing the latest version available. +3.Flexibility through polymorphism -xampp download +Riffing on this example, you now need just a few drivers, or functions, like “driveCar,” driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships, are unique. -Once downloaded and installed, in the case that the Windows operating system you will see the following screen, in which you will only have to start the Apache service. +This is where object-oriented programming’s sweet polymorphism comes into play. Because a single function can shape-shift to adapt to whichever class it’s in, you could create one function in the parent Car class called “drive” — not “driveCar” or “driveRaceCar,” but just “drive.” This one function would work with the RaceCarDriver, LimousineDriver, etc. In fact, you could even have “raceCar.drive(myRaceCarDriver)” or “limo.drive(myChauffeur).” -xampp app +4.Effective problem solving -## OOP Introduction +Object-oriented programming is often the most natural and pragmatic approach, once you get the hang of it. OOP languages allows you to break down your software into bite-sized problems that you then can solve — one object at a time. -Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods). +``` -## Project files +Look for disadvantages of this paradigm. -### [01 - Classes](./01-classes.php) +``` -The OOP paradigm encapsulates concepts of the real world in what is called as Classes which create Objects. In this file you will learn how to create a class and instanciate it. +1. Steep learning curve: The thought process involved in object-oriented programming +may not be natural for some people, and it can take time to get used to it. It is +complex to create programs based on interaction of objects. Some of the key +programming techniques, such as inheritance and polymorphism, can be +challenging to comprehend initially. -### [02 - Properties](02-properties.php) +2. Larger program size: Object-oriented programs typically involve more lines of code +than procedural programs. -Class member variables are called properties. In this file, you will learn how to add properties to a class and get them when the class is instantiated. +3. Slower programs: Object-oriented programs are typically slower than procedurebased programs, as they typically require more instructions to be executed. -### [03 - Methods](03-methods.php) +4. Not suitable for all types of problems: There are problems that lend themselves well +to functional-programming style, logic-programming style, or procedure-based +programming style, and applying object-oriented programming in those situations will +not result in efficient programs. -Properties define the characteristics of an object and the methods (functions in a class are called methods) which define the behavior of the Class. In this file you will learn how to create methods inside a class. +``` -### [04 - Getters](04-getters.php) +
+
-The get method returns the attribute value, usually there is a get method for each attribute of the class. In this file you will learn how to create **getter** methods. +# File analyzing -### [05 - Setters](05-setters.php) +### · File device.php -The set method sets the attribute value, usually there is a get method for each attribute of the class. In this file you will learn how to create **setter** methods. +
-### [06 - Constructors](06-constructors.php) +In this Class we have to create a logic in wiche the DeviceManager Class manage the +function getDeviceSerialNumber wiche is calling the function getSerialNumber() in the Device Class. +this triggers the echo of the serialNumber of the device. +
+
-A constructor allows you to initialize an object's properties upon creation of the object. In this file you will learn how to create the constructor method. +``` +· Create a class called Device. -### [07 - Inheritance problem](07-inheritance-problem.php) +· Then create another one called Mobile and then another one called Tablet. -There are several disadvantages of not applying inheritance in our code. In this file you will lean what's the problem if you don't apply any inheritance in your code. +· The Device has to define the common properties for a device, like, for example, +a method called getSerialNumber() which returns the Device serial number. -### [08 - Inheritance soluction](08-inheritance-solution.php) +· The Mobile and Tablet classes have to extend the Device class and define some particular +properties by their own. -The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. In this file you will learn how to apply the inheritance in your code. +· You should also create a class called DeviceManager. This class has to have a method +called getDeviceSerialNumber(Device $device) which has to call the getSerialNumber() method of +Device objects. -### [09 - Public, private & protected](09-public-private-protected.php) +· The DeviceManager Instance calls the getDeviceSerialNumber method with each one of +the devices objects. +``` -Properties and methods can have access modifiers which control where they can be accessed. In this file you will learn le three access modifiers. +### · File AbstractDevice.php -### [10 - Static](10-static.php) +
-Static properties and methods can be called directly - without creating an instance of the class first. In this file you will learn how to use static properties and methods. +In this Abstract Class Device we had to create three functions, the function getDatil is called by +the Mobile wiche extendes Device, and prints de ID of the mobile. +
+
-### [11 - Const](11-const.php) +``` +· Create an abstract class Device which defines an abstract method called getDetail($deviceId). -Constants cannot be changed once it is declared. Class constants can be useful if you need to define some constant data within a class. In this file you will learn how to create constants within a class. +· Create these two methods getId which returns the name of this device and getSerialNo +returns the serial number for this device. -### [12 - Abstract classes](12-abstract-classes.php) +· Now create a class which extends the Device and implements getDetail($deviceId) method. +Each device defines in its own way the details. That’s why it is abstracted to let the subclasses +implement this method. The other methods are common between devices. +``` -Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. In this file you will learn how to create and use abstract classes. +### · File InterfaceDevice.php -### [13 - Interfaces](13-interfaces.php) +
+We have 3 Classes DeviceManager, MemoryRepository, Device. +DeviceRepository is an Interface wiche MemoryRepository has to implement in order +to acces de createDevice() and findId() functions -Interfaces allow you to specify what methods a class should implement. -Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism". In this file you will learn how to create and extend interfaces. +MemoryRepository creates an Array wiche is empty ath the begining until we create an instance of DeviceManager and we create the objet wiche will be injected by the function addDevice(); +
+
-### [14 - Overriding](14-overriding.php) +Now let’s create an interface for a Device repository. -In function overriding, both parent and child classes should have same function name with and number of arguments. In this file you will learn how to implement overriding. +``` +· First create a Device class with some properties, as you like. -### [15 - Overloading](15-overloading.php) +· Then create an interface called DeviceRepository, which defines two methods create(Device $device) +which has an injection of the Device class you have created, and findById($deviceId) which with the +$deviceId returns the Device object but remember this is an interface do not implement methods, +it just defines them. -Function overloading contains same function name and that function preforms different task according to number of arguments. In this file you will learn how to implement overloading. +· Now we need an implementation of the interface DeviceRepository so we are going to create a +class called MemoryRepository which implements the DeviceRepository. Here you can define the methods +as you like. -### [16 - Namespaces](16-namespaces.php) +· Finally we need to create a class called DeviceManager, which defines a method +addDevice(DeviceRepository $repository, Device $device). -Namespaces are qualifiers that solve two different problems: +· Create an instance of it and inject to this method the implementation you have done. -1. They allow for better organization by grouping classes that work together to perform a task -2. They allow the same name to be used for more than one class +Remember this is to check your understanding about OOP. -In this file you will learn how to create and use namespaces. +``` diff --git a/01-classes.php b/exampels/01-classes.php similarity index 100% rename from 01-classes.php rename to exampels/01-classes.php diff --git a/02-properties.php b/exampels/02-properties.php similarity index 100% rename from 02-properties.php rename to exampels/02-properties.php diff --git a/03-methods.php b/exampels/03-methods.php similarity index 100% rename from 03-methods.php rename to exampels/03-methods.php diff --git a/04-getters.php b/exampels/04-getters.php similarity index 100% rename from 04-getters.php rename to exampels/04-getters.php diff --git a/05-setters.php b/exampels/05-setters.php similarity index 100% rename from 05-setters.php rename to exampels/05-setters.php diff --git a/06-constructors.php b/exampels/06-constructors.php similarity index 100% rename from 06-constructors.php rename to exampels/06-constructors.php diff --git a/07-inheritance-problem.php b/exampels/07-inheritance-problem.php similarity index 100% rename from 07-inheritance-problem.php rename to exampels/07-inheritance-problem.php diff --git a/08-inheritance-solution.php b/exampels/08-inheritance-solution.php similarity index 100% rename from 08-inheritance-solution.php rename to exampels/08-inheritance-solution.php diff --git a/09-public-private-protected.php b/exampels/09-public-private-protected.php similarity index 100% rename from 09-public-private-protected.php rename to exampels/09-public-private-protected.php diff --git a/10-static.php b/exampels/10-static.php similarity index 100% rename from 10-static.php rename to exampels/10-static.php diff --git a/11-const.php b/exampels/11-const.php similarity index 100% rename from 11-const.php rename to exampels/11-const.php diff --git a/12-abstract-classes.php b/exampels/12-abstract-classes.php similarity index 100% rename from 12-abstract-classes.php rename to exampels/12-abstract-classes.php diff --git a/13-interfaces.php b/exampels/13-interfaces.php similarity index 100% rename from 13-interfaces.php rename to exampels/13-interfaces.php diff --git a/14-overriding.php b/exampels/14-overriding.php similarity index 100% rename from 14-overriding.php rename to exampels/14-overriding.php diff --git a/15-overloading.php b/exampels/15-overloading.php similarity index 100% rename from 15-overloading.php rename to exampels/15-overloading.php diff --git a/16-namespaces.php b/exampels/16-namespaces.php similarity index 100% rename from 16-namespaces.php rename to exampels/16-namespaces.php diff --git a/mobileLibs.php b/exampels/mobileLibs.php similarity index 100% rename from mobileLibs.php rename to exampels/mobileLibs.php diff --git a/exercices/AbstractDevice.php b/exercices/AbstractDevice.php new file mode 100644 index 0000000..2be1db4 --- /dev/null +++ b/exercices/AbstractDevice.php @@ -0,0 +1,45 @@ +id = $id; + $this->brand = $brand; + $this->model = $model; + $this->serialNumber = $serialNumber; + } + + public function getId() + { + return $this->model; + } + + public function getSerialNumber() + { + return $this->serialNumber; + } + + public function getDetail($device) + { + return $device->id; + } +} + +class Mobile extends Device +{ + public function __construct(string $id, string $brand, string $model, string $serialNumber, string $capacity, string $price) + { + $this->capacity = $capacity; + $this->price = $price; + parent::__construct($id, $brand, $model, $serialNumber); + } + + public function getDetail($device) + { + return Device::getDetail($device); + } +} + +$mobile = new Mobile(5, 'Samsung', 'S20', 123456, '120GB', 1200); +var_dump($mobile->getDetail($mobile)); diff --git a/exercices/Device.php b/exercices/Device.php new file mode 100644 index 0000000..ae07b0b --- /dev/null +++ b/exercices/Device.php @@ -0,0 +1,64 @@ +brand = $brand; + $this->model = $model; + $this->serialNumber = $serialNumber; + } + + public function getSerialNumber() + { + return $this->serialNumber; + } +} + +class DeviceManager +{ + public function __construct(Device $device) + { + $this->serialNumber = $this->getDeviceSerialNumber($device); + } + + public function getDeviceSerialNumber(Device $device) + { + return $device->getSerialNumber(); + } +} + +class Mobile extends Device +{ + public function __construct(string $brand, string $model, string $serialNumber, string $capacity, string $price) + { + $this->capacity = $capacity; + $this->price = $price; + parent::__construct($brand, $model, $serialNumber); + } + + public function getMobileData() + { + return "
$this->brand
$this->model
$this->serialNumber
$this->capacity
"; + } +} + +class Tablet extends Device +{ + public function __construct(string $brand, string $model, string $serialNumber, string $capacity, string $price) + { + $this->capacity = $capacity; + $this->price = $price; + parent::__construct($brand, $model, $serialNumber); + } +} + +$mobile = new Mobile('Samsung', 'S20', '44352FH', '120GB', '1200'); +$tablet = new Tablet('IPad', 'Pro', 'IP45236', '256GB', '1800'); +$device = new DeviceManager($mobile); + + +echo ($device->getDeviceSerialNumber($mobile)); +echo "
"; +echo ($device->getDeviceSerialNumber($tablet)); +echo "
"; +echo $mobile->getMobileData(); diff --git a/exercices/InterfaceDevice.php b/exercices/InterfaceDevice.php new file mode 100644 index 0000000..54d0c6e --- /dev/null +++ b/exercices/InterfaceDevice.php @@ -0,0 +1,61 @@ +id = $id; + $this->brand = $brand; + $this->model = $model; + $this->serialNumber = $serialNumber; + } +} + +class MemoryRepository implements DeviceRepository +{ + public $devices = array(); + + public function createDevice(Device $device) + { + $this->devices[] = $device; + } + public function findId($deviceId, $array) + { + if (in_array($deviceId, $array)) { + return $deviceId; + } + } +} + + +class DeviceManager extends MemoryRepository +{ + function __construct(MemoryRepository $repository, Device $device) + { + $this->repository = $repository; + $this->device = $device; + } + + function addDevice($repository, $device) + { + $repository->createDevice($device); + return $repository; + } +} + +$device1 = new Device(1, 'Samsung', 'S20', 'SG3454'); +$device2 = new Device(2, 'iPhone', 'X', 'IP7849'); +$device3 = new Device(3, 'Xiamoi', 'MI30', 'XM0948'); + +$memory = new MemoryRepository(); +$addDevice = new DeviceManager($memory, $device2); +$addDevice->addDevice($memory, $device1); +$addDevice->addDevice($memory, $device2); + +var_dump($memory); From 532f23051cb54d6f8a7728808e70932e5a78481c Mon Sep 17 00:00:00 2001 From: Hayk Petrosyan Date: Mon, 19 Jul 2021 22:35:28 +0200 Subject: [PATCH 2/2] Formating README --- README.md | 122 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 93 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index a250c11..ffe2203 100644 --- a/README.md +++ b/README.md @@ -14,47 +14,68 @@ In the _"exercices"_ folder you will find the files with the Classes and functio What is object-oriented programming in general terms? ``` -Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior. +Object-oriented programming (OOP) is a computer programming model that organizes +software design around data, or objects, rather than functions and logic. An object +can be defined as a data field that has unique attributes and behavior. -OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. +OOP focuses on the objects that developers want to manipulate rather than the +logic required to manipulate them. ``` What is a class? ``` -In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). +In object-oriented programming, a class is a blueprint for creating objects +(a particular data structure), providing initial values for state +(member variables or attributes), and implementations of behavior +(member functions or methods). -The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code. +The user-defined objects are created using the class keyword. The class is a +blueprint that defines a nature of a future object. An instance is a specific +object created from a particular class. Classes are used to create and manage +new objects and support inheritance—a key ingredient in object-oriented programming +and a mechanism of reusing code. ``` What is an object? ``` - An Object is an individual instance of the data structure defined by a class. We define a class once and then make many objects that belong to it. Objects are also known as instances. + An Object is an individual instance of the data structure defined by a class. + We define a class once and then make many objects that belong to it. Objects + are also known as instances. ``` What is an instance? ``` -An instance is a concrete occurrence of any object, existing usually during the runtime of a computer program. Formally, "instance" is synonymous with "object" as they are each a particular value (realization), and these may be called an instance object; "instance" emphasizes the distinct identity of the object. The creation of an instance is called instantiation. +An instance is a concrete occurrence of any object, existing usually during the +runtime of a computer program. Formally, "instance" is synonymous with "object" +as they are each a particular value (realization), and these may be called an +instance object; "instance" emphasizes the distinct identity of the object. +The creation of an instance is called instantiation. ``` What is a property? ``` -Properties are as in everyday language and technically are fields of objects/classes with dedicated getter/setter routines (which can be considered as methods). +Properties are as in everyday language and technically are fields of objects/classes +with dedicated getter/setter routines (which can be considered as methods). ``` What is a method? ``` -Methods ("member functions") are similar to functions, they belongs to classes or objects and usually expresses the verbs of the objects/class. For example, an object of type Window usually would have methods open and close which do corresponding operations to the object they belong. +Methods ("member functions") are similar to functions, they belongs to classes or +objects and usually expresses the verbs of the objects/class. For example, an object + of type Window usually would have methods open and close which do corresponding + operations to the object they belong. ``` What is the difference between a function and a method? ``` -A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not. +A method, like a function, is a set of instructions that perform a task. +The difference is that a method is associated with an object, while a function is not. ``` @@ -72,21 +93,32 @@ What is the difference between a class, an object and an instance? ``` The Class is a “template” / “blueprint” that is used to create objects. -Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). They consist of state and related behavior wiche is given by the Class. -An instance is a unique copy of a Class that representing an Object, wiche is given by the constructor of the class Object. When a new instance of a class is created, the proccesing engine will allocate a room of memory for that class instance. +Real world objects shares 2 main characteristics, state and behavior. Human +have state (name, age) and behavior (running, sleeping). They consist of state +and related behavior wiche is given by the Class. An instance is a unique +copy of a Class that representing an Object, wiche is given by the constructor +of the class Object. When a new instance of a class is created, the proccesing +engine will allocate a room of memory for that class instance. ``` What do we understand about the concept of encapsulation? ``` -Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them +Encapsulation is one of the fundamentals of OOP (object-oriented programming). +It refers to the bundling of data with the methods that operate on that data. +Encapsulation is used to hide the values or state of a structured data object +inside a class, preventing unauthorized parties’ direct access to them ``` What do we understand about the concept of abstraction? ``` -Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. +Abstraction is one of the key concepts of object-oriented programming (OOP) +languages. Its main goal is to handle complexity by hiding unnecessary details +from the user. That enables the user to implement more complex logic on top of +the provided abstraction without understanding or even thinking about all the +hidden complexity. ``` What do we understand about the concept of inheritance? @@ -102,61 +134,92 @@ extend keyword What do we understand about the concept of polymorphism? ``` -Polymorphism is the method in an object-oriented programming language that performs different things as per the object's class, which calls it. With Polymorphism, a message is sent to multiple class objects, and every object responds appropriately according to the properties of the class. +Polymorphism is the method in an object-oriented programming language that +performs different things as per the object's class, which calls it. With Polymorphism, +a message is sent to multiple class objects, and every object responds appropriately +according to the properties of the class. ``` What do we understand about the concept of Overload? ``` -AnswerOverloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. +AnswerOverloading happens when you have two methods with the same name but +different signatures (or arguments). In a class we can implement two or more +methods with the same name. ``` What do we understand about the concept of Override? ``` -Overriding is a feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes. +Overriding is a feature that enables a child class to provide different +implementation for a method that is already defined and/or implemented in +its parent class or one of its parent classes. ``` What differences exist between the concept of Overload and Override? ``` -1. Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism. +1. Overriding implements Runtime Polymorphism whereas Overloading +implements Compile time polymorphism. -2. The method Overriding occurs between superclass and subclass. Overloading occurs between the methods in the same class. +2. The method Overriding occurs between superclass and subclass. Overloading +occurs between the methods in the same class. -3. Overriding methods have the same signature i.e. same name and method arguments. Overloaded method names are the same but the parameters are different. +3. Overriding methods have the same signature i.e. same name and method arguments. +Overloaded method names are the same but the parameters are different. -4. With Overloading, the method to call is determined at the compile-time. With overriding, the method call is determined at the runtime based on the object type. +4. With Overloading, the method to call is determined at the compile-time. +With overriding, the method call is determined at the runtime based on the object type. -5. If overriding breaks, it can cause serious issues in our program because the effect will be visible at runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix. +5. If overriding breaks, it can cause serious issues in our program because +the effect will be visible at runtime. Whereas if overloading breaks, the +compile-time error will come and it’s easy to fix. ``` What is a static class? ``` -Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor. However, they can contain a static constructor. +Static classes are sealed and therefore cannot be inherited. They cannot inherit +from any class except Object. Static classes cannot contain an instance constructor. +However, they can contain a static constructor. ``` -Look for 3 advantages over object-oriented programming compared to other programming paradigms +Look for 3 advantages over object-oriented programming compared to other +programming paradigms ``` 1.Modularity for easier troubleshooting -When working with object-oriented programming languages, you know exactly where to look. “Oh, the car object broke down? The problem must be in the Car class!” You don’t have to muck through anything else. +When working with object-oriented programming languages, you know exactly +where to look. “Oh, the car object broke down? The problem must be in the Car class!” +You don’t have to muck through anything else. 2.Reuse of code through inheritance -Suppose that in addition to your Car object, one colleague needs a RaceCar object, and another needs a Limousine object. Everyone builds their objects separately but discover commonalities between them. In fact, each object is really just a different kind of Car. This is where the inheritance technique saves time: Create one generic class (Car), and then define the subclasses (RaceCar and Limousine) that are to inherit the generic class’s traits. +Suppose that in addition to your Car object, one colleague needs a RaceCar object, +and another needs a Limousine object. Everyone builds their objects separately but +discover commonalities between them. In fact, each object is really just a different +kind of Car. This is where the inheritance technique saves time: Create one generic +class (Car), and then define the subclasses (RaceCar and Limousine) that are to +inherit the generic class’s traits. 3.Flexibility through polymorphism -Riffing on this example, you now need just a few drivers, or functions, like “driveCar,” driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships, are unique. +Riffing on this example, you now need just a few drivers, or functions, like “driveCar,” +driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with LimousineDrivers, +but other things, like RaceHelmets and BeverageSponsorships, are unique. -This is where object-oriented programming’s sweet polymorphism comes into play. Because a single function can shape-shift to adapt to whichever class it’s in, you could create one function in the parent Car class called “drive” — not “driveCar” or “driveRaceCar,” but just “drive.” This one function would work with the RaceCarDriver, LimousineDriver, etc. In fact, you could even have “raceCar.drive(myRaceCarDriver)” or “limo.drive(myChauffeur).” +This is where object-oriented programming’s sweet polymorphism comes into play. Because a +single function can shape-shift to adapt to whichever class it’s in, you could create one +function in the parent Car class called “drive” — not “driveCar” or “driveRaceCar,” +but just “drive.” This one function would work with the RaceCarDriver, LimousineDriver, etc. +In fact, you could even have “raceCar.drive(myRaceCarDriver)” or “limo.drive(myChauffeur).” 4.Effective problem solving -Object-oriented programming is often the most natural and pragmatic approach, once you get the hang of it. OOP languages allows you to break down your software into bite-sized problems that you then can solve — one object at a time. +Object-oriented programming is often the most natural and pragmatic approach, once you +get the hang of it. OOP languages allows you to break down your software into bite-sized +problems that you then can solve — one object at a time. ``` @@ -173,7 +236,8 @@ challenging to comprehend initially. 2. Larger program size: Object-oriented programs typically involve more lines of code than procedural programs. -3. Slower programs: Object-oriented programs are typically slower than procedurebased programs, as they typically require more instructions to be executed. +3. Slower programs: Object-oriented programs are typically slower than procedurebased +programs, as they typically require more instructions to be executed. 4. Not suitable for all types of problems: There are problems that lend themselves well to functional-programming style, logic-programming style, or procedure-based