What is Data Abstraction??

IMAGINE

Simply driving a steering wheel without understanding how the parts inside work. This is exactly what Data Abstraction is.

Data Abstraction simplifies complex systems by showing only the essential features and hiding complex details, so users can easily understand and interact with systems or apps!

It focuses on *what* something does rather than *how* it does it.

Example #1: Rounding numbers

One example is built-in functions in JavaScript, because they allow you to use one simple, clear command to run a type of function, but don’t show how the built-in function was coded.

// CODE_RUNNER: Try out Data Abstraction with Math.round()

// Abstraction in Action: Math.round()

// The Complex Data (Input)
const messyNumber = 7.84236;


// We call the built-in function. 
// We don't care *how* it calculates the rounding

// only that it *does* the rounding
const roundedResult = Math.round(messyNumber);

// The Simple Result (Output)
console.log(`The original messy number was: ${messyNumber}`);
console.log(`The simplified, rounded result is: ${roundedResult}`);
// Expected output: 8

<IPython.core.display.Javascript object>

What’s hidden:

The internal logic the JavaScript uses to look at the decimal part (.84236), determine it’s >= 0.5, and then round the integer part (7 -> 8). All of this is abstracted away!

Example #2: Adding two numbers

The user should be able to call addNumbers(a, b) and trust it works without needing to see the math inside.

YOUR JOB:

The code below is “broken”. Complete the function so that it successfully returns the sum of the two inputs.

hint:

use the return keyword inside the function

// CODE_RUNNER: Display two numbers, and add them!

// The Abstraction (The Function)
function addNumbers(num1, num2) {
    // Add code here
}

let a = 2
let b = 3

// Use the abstraction to get the result
let result = addNumbers(a, b);

console.log(result);
<IPython.core.display.Javascript object>

Data Abstraction with Classes

Secondly, Data Abstraction can be seen in Object Oriented Programming, specifically with Classes and Objects . Classes are essential tools for simplifying how we handle data.

Example #3: Checking Account Balance

// CODE_RUNNER: Use the abstraction to deposit $200
class BankAccount {
    constructor() {
        this.balance = 500;
    }

    deposit(amount) {
        this.balance = this.balance + amount;
        console.log("đź’° Deposited: $" + amount);
    }
}

const myAccount = new BankAccount();

// Write code here
// Hint: myAccount.______();

// 3. Check the result
console.log("Final Balance: $" + myAccount.balance);

Inheritance

In programming, Inheritance allows one class (called a subclass / child class) to "borrow" features (properties and methods) from another class (the adult).

Real World Example: Smartphone

The Parent Class: A basic phone that can make calls

The Child Class: An iPhone that inherits the ability to make phone calls, but adds its own features

// CODE_RUNNER: Use the inherited methods to make a call and take a photo
class Phone {
    makeCall() {
        console.log("Making a phone call");
    }
}

// Child class
class SmartPhone extends Phone {
    takePhoto() {
        console.log("Taking a photo");
    }
}

const myNewPhone = new SmartPhone();

// Hint: const_name.method();

HOMEWORK

  1. This is a code of a calculator. Please read through the code first to try and understand what it does. There are many unnecessary lines within the code. Please identify the unnecessary lines and delete them.
/* I removed...
- unused variables (ex: nothing, randomText, temp)
- logic/loops surrounding temp and nothing
- result = result (redundant)
- randomText = not really used anywhere
*/

function calculator(num1, num2, operator) {
    let result;

    if (operator === "+") {
        result = num1 + num2;
    } else if (operator === "-") {
        result = num1 - num2;
    } else if (operator === "*") {
        result = num1 * num2;
    } else if (operator === "/") {
        result = num1 / num2;
    } else {
        result = "Invalid operator";
    }

    return result;
}

console.log(calculator(10, 5, "+"));
console.log(calculator(10, 5, "-"));
console.log(calculator(10, 5, "*"));
console.log(calculator(10, 5, "/"));
<IPython.core.display.Javascript object>
  1. Below a pet class is given. Your Job is to add a new subclass, Dog, that inherits from the Pet Class
%%js 

// CODE_RUNNER: Add a Dog subclass
class Pet {
    eat() {
        console.log("Nom nom nom");
    }
}

class Dog extends Pet {
    bark() {
        console.log("Woof woof!");
    }
}
// all Dog instances can use the methods from Pet unless specfifically overridden

const myDog = new Dog();
myDog.eat();  // Inherited method from the boiler code
myDog.bark(); // Method defined in Dog class that I created
<IPython.core.display.Javascript object>