Ticker

6/FOX NEWS/ticker-posts

Ad Code

Responsive Advertisement

A Beginners Guide To Abstraction in Object-Oriented Programming

A Beginners Guide To Abstraction in Object-Oriented Programming Learns the basics of abstraction in object-oriented programming with example code and practice challenges.

When you eat a burrito, every one of your taste receptors sings with joy. Each bite brings in a combination of different flavor types, like salty, sweet, spicy, and umami. Every bite after that tastes slightly different as a new collection of ingredients join their flavors together.

A Beginners Guide To Abstraction in Object-Oriented Programming

You may have read the ingredients from the menu, but you don’t know exactly how the salsa gets made. The seasoning they use on their veggies could be a secret blend. You don’t need to know every exact ingredient, though. It’s enough to know that it’s delicious.

The ingredients you know, like plain white rice, are concrete. The other elements are abstract. You know it’s salsa, but what kind? Or, if someone just hands you a burrito out of nowhere, then the whole burrito is abstract.

Abstraction In The Abstract

Along with inheritance, abstraction is an important concept in object-oriented programming. The theory is that every object should deliver simple and predictable results. Objects should also only share what needs to be shared.

Abstraction Keeps Code And Data Hidden When Appropriate

You can think of a burrito as an object. Inside the burrito, you have several other objects, like beans, rice, cheese, and hot sauce. The beans may have been seasoned. The cheese could be a blend. And the hot sauce might be a combination of peppers aged in vinegar.

You don’t need to know how all the ingredients in a burrito were made. And in the case of hot dogs, you probably don’t want to know. All that matters is that it doesn’t fall apart when you eat it and that it’s super tasty.

Abstraction Is Closely Tied To Encapsulation

It’s the same thing with programming objects. When you instantiate an object (create it from a class), it’s like ordering a burrito from the food truck counter. You have access to some data, but not all. You don’t need to know how the object works, as long as the functions return the correct data. Here is a burrito in JavaScript/Typescript:

class CheeseBlend {
private _ingredients = [“Colby Jack”, “Cheddar”, “Manchego”];
get ingredients() {
return “melted cheese”;
}
}
class SecretSalsa {
private _ingredients = [“onions”, “tomatoes”, “cilantro”, “Guatemalan Insanity Peppers”];
get ingredients() {
return “it’s a secret”;
}
}
class Burrito {
private _beans = “beans”;
private _rice = “rice”;
private _cheese: CheeseBlend = new CheeseBlend();
private _salsa: SecretSalsa = new SecretSalsa();
get cheese() {
return this._cheese.ingredients;
}
get salsa() {
return this._salsa.ingredients;
}
}
let burro = new Burrito();
console.log(burro.cheese);
console.log(burro.salsa);

You can play with this code at the TypeScript sandbox.

In the above example, the salsa and cheese ingredients are abstracted away. First, they’re encapsulated, to hide the special ingredients. Then getters are added to access the ingredients. But the ingredients only return an abstract representation of what they really are.

READ More:www.makeuseof.com/beginners-abstraction-object-oriented-programming/

The post A Beginners Guide To Abstraction in Object-Oriented Programming appeared first on How To Get Job.



from WordPress https://howtogetafreejob.com/a-beginners-guide-to-abstraction-in-object-oriented-programming/
Reactions

Post a Comment

0 Comments