---
# System prepended metadata

title: 04 Object Oriented Progamming - demo
tags: [Device Programming]

---

# 04 Object Oriented Progamming - demo

https://github.com/howest-mct/DEVPROG_DEMO_04

[toc]

 

## Demo: Goal

We wish to create a drink card app. All kinds of Drinks have similar properties and behavior, however, some specific types of drinks will also differ from each other. For example, Wine will have a specific WineType (red, white,…), while other Drinks don’t. Also, they might have different serving instructions. 

We create a **base class** called ‘Drink’ that contains common properties such as Name and Price. 

The main goal is to create global list that contains all kinds of drinks, which should be automatically displayed using their own specific behavior (or the main behavior if no specific behavior was described).


![](https://hackmd.io/_uploads/B1zrEOemo.png)


## Replacing behavior 

Most drinks are served by opening the bottle and pouring the content in a glass, for that, Drink gets the `ShowServingInstructions()` method. Wine however, requires a special glass, should be opened in front the client and someone should taste it first. This is a whole new implementation of the serving. 

* We need a **common behavior** for Drinks 
* We need to be **able (not required!) to replace** that common behavior 
* Solution: we make the method **virtual**   in the base class and override it only if necessary

![](https://hackmd.io/_uploads/SJh9LuxQj.png) 
*This ‘virtual’ keyword can be used for both methods (as in the example) and properties!*


## Adding behavior

There are two ways to add behavior (override virtual + base, override abstract). 

* We want to add behavior for **specific types** 
    * **using a subclass**
* We have common behavior in the base class that we wish to **extend** with extra behavior?
    * **virtual**
* We don’t have common behavior in the base class but want to **force** the subclasses **to implement** specific behavior
    * **Interface or abstract**

### Using a subclass 

==**Situation:**== We want to add behavior for **specific types**.

* **How**: add properties and methods in the subclass
* **Example**: Only Wine contains data for the Country of origin, a list of grapes, a different price for the whole bottle instead of just a glass, and a wine type (red, white,…).
* **Solution**: we add properties in the Wine class that are not in the Drink base class.

![](https://hackmd.io/_uploads/By1jwul7o.png)


#### Virtual 

==**Situation:**== We have common behavior in the base class that we wish to **extend** with extra behavior.

* **How**: the base class contains an implementation for the behavior (property/method). The subclass extends this by overriding the behavior but also **calls the base** behavior
* **Example**: Most drinks are to be shown by the name of the product (eg.: coca-cola). The name of the Wines, however, should be preceded by the type (“RED”, “WHITE”,…) 
* **Solution**: we override the property, call the base class and add extra behavior.

![](https://hackmd.io/_uploads/B11M_deXj.png)
![](https://hackmd.io/_uploads/HkMzuOeXo.png)



### Interface / abstract 

==**Situation:**== We don’t have common behavior in the base class but want to **force** the subclasses **to implement** specific behavior.

* **Abstract - how**: the base class contains an **abstract** function header or property, which by consequence has no implementation. All subclasses **must** implement this function or they get an error. Abstract properties and methods can exist in a default or abstract class, together with non-abstract ones.
* **Interface - how**: an interface is a type of class that contains **no implementation** for any properties or methods. Every class that implements (not: inherits) this interface **must** provide an implementation for all properties and methods in this interface, or they get an error.
* **Interfaces and abstract CLASSES** make sure that the user cannot instantiate the base class. 
For example, if I have an IDrink interface, or an abstract class Drink, the user will **not** be able to do as follows:
`Drink drink = new Drink();` 
Which is good, since ‘Drink’ itself does not exist in this case. However, polymorphism will allow them to do this: 
`Drink drink = new Soda();`
_For more information on this, we refer to the explanation in the theory lesson._ 
* **Example - abstract**: every specific type of drink gets a ‘sign’ (could be an icon, or in this case: B for Beer, W for Wine, …). Since ‘Drink’ does not exist, we have no specific sign for it.
* **Solution - abstract**: we create an abstract property ‘Sign’ with only a getter, forcing all subclasses to provide an implementation for this property. In this way, using polymorphism, we can display every type of drink with its sign. 
Also, we make the Drink class itself abstract so that it cannot be instantiated. 
![](https://hackmd.io/_uploads/r1nTtugmj.png)
![](https://hackmd.io/_uploads/SklCt_l7i.png)
* **Example - interface**: we wish to use some of the drinks in another app that visualizes all kinds of collectable items (wines, comic books, etc.). What is important for that app is the year of origin.
* **Solution** - interface: we make an interface that can be implemented by all kinds of collectable items. 
![](https://hackmd.io/_uploads/S1Gb9_e7o.png)
![](https://hackmd.io/_uploads/r1UG9Ol7o.png)
![](https://hackmd.io/_uploads/r1FM9_g7s.png)

## Inheritance vs Object composition 
Below you can find a few examples from the demo. For further explanation on this topic, we refer to the theory lessons.

### Inheritance
Inheritance defines a **… is a …** relationship. 
* Example: a Cocktail **is a** Drink, a Wine **is a** Drink, …
    ![](https://hackmd.io/_uploads/H1sI9Oems.png)
    ![](https://hackmd.io/_uploads/SyxP5uxQj.png)

### Object composition
Object composition defines a **… has a …** relationship, resulting in a **property** of that type. 
* Example: a Cocktail **has a** list of ingredients (of type Drink)
    ![](https://hackmd.io/_uploads/Hkgccul7s.png)

