Is this a valid abstraction example ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is this a valid abstraction example ?

I've just started to learn Object Oriented Programming with Java . And I'm dealing with one of the fundamental concepts , abstraction. I've not learned abstract classes yet , I have just learned the ideology. I understand , via abstraction , we tend to provide basic idea of what the class is , and what it is doing . I've tried to form a basic example . Can you help me whether it is a valid example ? https://code.sololearn.com/cShFXnFAPLGm/#java This is my public code named as "Abstraction" .

3rd Mar 2019, 12:39 PM
Sevcan Doğramacı
Sevcan Doğramacı - avatar
5 Answers
+ 3
Yep. The example is correct. Abstraction simply means data hiding and making it accessible via methods. You don't need a Button class - you can do all things in this 1. And some suggestions. void goUp { currentStep++; } void goDown { currentStep--; } int getCurrentStep { retur currentStep; }
3rd Mar 2019, 12:43 PM
Seniru
Seniru - avatar
+ 2
Seniru Pasan Thanks a lot for your help and suggestions !
3rd Mar 2019, 12:54 PM
Sevcan Doğramacı
Sevcan Doğramacı - avatar
+ 1
The idea of an abstract class is that it tells you what's available, but not how it's implemented. So in your example, you could have an abstract class of Button: abstract class Button { abstract void goToFloor(); abstract void ringAlarm(); abstract void goUp(); abstract void goDown(); } When your Elevator class inherits from Button, it's going to receive the abstract method goToFloor, and then it's up to you to decide what it does and how it works. It's saying that this is a method you should probably have, but you can make it do what you want. For example, maybe in this particular Elevator, goUp ascends two floors instead of just one. The methods are abstract because maybe you want to make another Elevator where goUp ascends five floors. Maybe you have a ton of elevators to define, but you know they're all going to need the methods in the Button class, even though you want them to do different things.
7th Mar 2019, 6:29 PM
Adam Neidlinger
0
Thank you for the information for abstract classes !
8th Mar 2019, 9:38 PM
Sevcan Doğramacı
Sevcan Doğramacı - avatar
0
Can anyone post a program on abstraction used in ATM
25th Nov 2021, 2:38 PM
Rishi Reddy
Rishi Reddy - avatar