Abstraction : OOPs concept and its example

What is abstraction in object oriented programming?

Abstraction is the process of implementing the essential features and removing the unnecessary information. Abstraction is an important characteristic of abstraction in which the programmers hide the background details and show important one. This will help in reducing the size of the code and the complexity of the code.

Abstraction is implemented through Interface and abstract classes in Java. You may like to read more Encapsulation in OOPs and benefits of using OOPs.

Real world example of abstraction

1) Mobile phones
We use mobile phone and we are unaware of the internal functionality of the mobile phones. Feature of calling is like we dial a number which will concatenate the sequence of the digits and form ten digit numbers. And finally it gets displayed on the screen. And when we press the green button then call gets connected to the person. We are using mobile phone features but we are not aware of internal functionalities behind it. This is possible only through abstraction.

2) GMAIL 
When we enter our user Id and password on Gmail login page it automatically calls the home page through which we can compose mails, view and send mails etc. Here also users do not know the internal functionality of features of Gmail which are unnecessary for them and are hidden. Users know only about the features which are necessary information for them.

Abstraction example through programming language (JAVA)


Interface MobilePhones
{
Public void calling();
Public void sendSMS();
}
Class MOTOG implements MobilePhones
{
void calling()
{
System.out.println(“MOTOG PHONE HAS BEEN CALLED”);
}
Void sendSMS()
{
System.out.println(“MESSAGE HAS BEEN SEND TO MOTOG”);
}
}
Class NOKIA implements MobilePhones
{
void calling()
{
System.out.println(“NOKIA PHONE HAS BEEN CALLED”);
}
Void sendSMS()
{
System.out.println(“MESSAGE HAS BEEN SEND TO NOKIA”);
}
}
Class MOTOX implements MobilePhones
{
void calling()
{
System.out.println (“MOTOX PHONE HAS BEEN CALLED”);
}
Void sendSMS()
{
System.out.println (“MESSAGE HAS BEEN SEND TO MOTOX”);
}
}

Here we have created mobilePhones interface and other classes call it and implement it according to their own features. Three classes MOTOG, MOTOX and NOKIA implement the interface according their own features.

Advantage of abstraction

  • Abstraction makes code readable and simpler to understand. The codes are simple to understand as the complexity of the code is hidden from the user.
  • The size of the code is reduced as through abstraction we do not show unnecessary codes.
  • If we don’t apply abstraction to codes then it is difficult to maintain the codes.
  • Through abstraction, we simplify the representation of Domain Model.
  • Abstraction grouped the data of same type together and separate different type.

Read More:
Difference between portability and compatibility testing
What is the difference between test cases and test scenarios?

Copyright © ianswer4u.com

0 Reactions:

Post a Comment