Abstraction in Object-Oriented Programming: Benefits and Examples

Introduction

What is an abstraction in OOP?

Ok, but can you explain abstraction to me in simple terms?

Photo by Shaira Dela Peña on Unsplash

Girlfriend and Dog

public class Girlfriend {

public void loveMe() {
// Give presents and flowers
// Spend time talking about hopes and dreams
}
}


public class Dog {

public void loveWoofWoof() {
// Take dog outside to play
// Bathe and groom the dog
// Run around trying to get the dog to chase
}
}
public class Hatef {

public void showLoveToGirlFriend(GirlFriend gf){
gf.loveMe();
}

public void showLoveToDog(Dog dog){
dog.loveWoofWoof();
}

}
public interface Lovable {
void love();
}
public class Girlfriend implements Lovable {

@Override
public void love() {
// Give presents and flowers
// Spend time talking about hopes and dreams
}
}


public class Dog implements Lovable {

@Override
public void love() {
// Take dog outside to play
// Bathe and groom the dog
// Run around trying to get the dog to chase
}
}
public class Hatef {

public void showLove(Lovable lovely){
lovely.love();
}
}

Benefits of Abstraction

What if We Don’t Use Abstraction?

Conclusion

Coding with abstraction, creating a great life,
Java is the language that is so full of strife,
For the complex tasks and complex minds,
Abstraction makes life of all kinds.

Strings and loops and numbers, too,
Objects made with abstraction through and through,
Classes floating on the wings of the night,
Producing the power to do what is right.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store