Difference between Abstraction and Encapsulation

Earlier we discussed about basics of Object oriented programming , what abstraction in OOPs is and what encapsulation means . Today let us see what differentiates abstraction from encapsulation.

  1. Abstraction is implemented through Interface and abstract classes in Java. While encapsulation is implemented by using access modifiers like private, public and protected.
  2. Encapsulation is also called data hiding. That is hiding the details from outside the world.
  3. Abstraction is based on design principal of “programming for interface than implementation” and basis of encapsulation is “encapsulate whatever changes”.
  4. Abstraction resolves the problem at the design level but the encapsulation resolves the problem at the implementation level.
  5. Abstraction hides the irrelevant details and gives relevant information. Encapsulation is encapsulating data and functions into single unit so that it can be protected from outside the world.
  6. Abstraction focuses on what the object does rather than how it does. Encapsulation hides the internal features which implement the functionality of the class.
  7. Example of abstraction is “Outer look of the mobile like the display screen on which the numbers are displays and keypads on which the digits and other character are displayed”. Example of encapsulation is inner layout of the mobile phone like how the how the keypad and its different buttons are connected with the screen.
Copyright © ianswer4u.com

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

Encapsulation in Object Oriented Programming

Encapsulation is a concept in object oriented programming which focuses on protecting the variable and function from outside the world in order to manage the piece of code in better way and produce least impact on other parts of the code. Read More about OOPS concepts, features of object oriented languages and their benefits.

Encapsulation supports the mechanism of binding of parameter and functions together. The binding of parameter and functions can be done in a class.

Important points about encapsulation

  • Through encapsulation it is possible to hide the internal details of how an object does something in a class. Encapsulation helps in resolving the problem at the implementation level.
  • With defining a class or structure it is possible to define the accessibility of the members (methods, properties, variables) to the code outside it. Encapsulation can thus help us control the interaction between objects
  • The data of an object can be used by another object without knowing the entire data. For example, an Employee object may have name, address, company, and department as its properties. If a third party object wants to use the Employee object, it can request the name and address for the employee without needing to know the company and department details of the object.

Thus a great benefit of encapsulation is that it can change the internal implementation of a class without altering the overall system.

Let us understand encapsulation with the help of example

Class College
{
//creation of private members
private String collegeId;
private String collegeName;
private String collegeLocation;
private Long collegeContact;

//creation of private constructor
private college( String collegeid, String collegeName, String collegeLocation,collegeContact)
this.collegeid=collegeid;
this.collegeName=collegeName;
this.collegeLocation=collegeLocation;
this.collegeContact =collegeContact;

//create college can encapsulate inside loan creation module
Public College createCollege()
{
return College;
}
}

In the above code, we have made all the member type as private so that they are encapsulated and we can update and access these variables only inside the class and nowhere else.

Design pattern used based on encapsulation

1) Singleton design pattern
Singleton design pattern is used to create an object with help of method get instance (). Since the object is created inside one class and not from any other place in code you can easily change how you create object without affecting the other part of code.

2) Factory design pattern
Factory design pattern is used for creation of the design pattern which is used to create objects other than creating object through new operator.

Benefits of Encapsulation

  1. Encapsulated codes in object oriented programming are better and more flexible to use.
  2. Encapsulated codes in object oriented programming can be easily adaptable to new changes.
  3. Encapsulated codes can restrict the user access.
  4. Encapsulated code in object oriented programming reduces coupling of the modules and increases cohesion because all the codes are encapsulated together.
  5. If we change anything in the encapsulated code then rest part of code remains unaffected.
  6. Through encapsulation, we can change the implementation code without breaking the code of others
  7. In encapsulation we hide the implementation details behind a public programming interface.
  8. Encapsulation makes unit testing easy.

Read More :
Advantages And Disadvantages Of Peer To Peer Network
What are Proxy Servers and their uses ?
Benefits of Beta Testing


Copyright © ianswer4u.com

OOPS : Advantages and Features Of Object Oriented Programming

What is object oriented programming?

Object oriented programming language (OOPL) is a high level programming language based on objected oriented programming model (OOPM). Object oriented programming comprises of logical classes, objects, methods and relationships.
Object oriented programming is a programming language that focuses on objects (i.e. data) rather than logic (i.e. procedures). If we talk about structured programming language then it focuses on procedures and not on the data. So in object oriented programming we focus on how we can manipulate the object rather than logic which are used to manipulate data of the object. Each object is independent of each other and executed independently. It can execute by itself and can be interchanged with other objects. Object interacts by interchanging information with each other.

Advantages of Object Oriented Programming

  1. Objects are created on real world entities. 
  2. Through object oriented programming language, complex systems of real world can be converted into software solutions. 
  3. Large programs are difficult to develop. Objected oriented programs focuses the developer to do extensive planning which will reduces programming flaws and better design. 
  4. Software maintenance of the object oriented programming is easier than as compared with structured oriented programming as much time has been invested while doing the planning of the code so minimum flaws are found. 
  5. Object oriented programming can be easily reused in other programs due to its re-usability feature. 
  6. Objected oriented programming is easy to implement. 
  7. Object oriented programming can easily extensible. New features or changes in operating environment can be easily done.
 Let us discuss some of the basic characteristics of OOPS in brief, subsequently I will discuss them in detail.

Basic features of object oriented programming language:

 Encapsulation
Encapsulation is a concept in object oriented programming which focuses on protecting the variable and function from outside the world in order to manage the piece of code in better way and produce least impact on other part of the code.

Important points about encapsulation
  • The internal details about what the object does can be hidden by using encapsulation. The problem can be solved at the implementation level itself.
  • With defining a class or structure it is possible to define the accessibility of the members (methods, properties, variables) to the code outside it. Encapsulation can thus help us control the interaction between objects.
  • The data of an object can be used by another object without knowing the entire data. For example, an Employee object may have name, address, company, and department as its properties. If a third party object wants to use the Employee object, it can request the name and address for the employee without needing to know the company and department details of the object.
Thus a great benefit of encapsulation is that it can change the internal implementation of a class without altering the overall system.

 Abstraction
Abstraction is the process of implementing the essential features and removing unnecessary information. Abstraction is an important characteristic in which the programmer hides the irrelevant details which will in turn reduces the size of the code and the complexity of the code. Abstraction makes things more general, simpler and abstract.

Important points about 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. 

 Inheritance
Inheritance is another main feature of object oriented programming language. Inheritance allows the class to use the property and method of other class. In other words, we can say that the sub classes derive the property and functionality from the base class. Subclass is also known as derived class and base class is known as super class.

The important points about inheritance
  • Inheritance allows the developers to reuse the codes so that time and effort can be saved.
  • A subclass can be treated as if it is a super class.
  • It is possible to create a class of extended and exclusive functionality without having to change the functionalities and properties of the super class.
  • Through inheritance the relationships among objects(of sub class and super class) can easily be established 

 Polymorphism
Polymorphism is way by which we are defining multiple functionalities under the same names. In other words we can say that we have the same code or operation but it will behave differently in different contexts.

Some points to remember about polymorphism
  • A functionality can behave differently for different instances
  • The behaviour depends on the type of data used in the operation
  • Polymorphism is used for implementing inheritance. 

Read  More
Difference between Automated Testing and Manual Testing
Drivers and Stubs in Software testing
Benefits and drawbacks of whitebox testing

  Copyright © ianswer4u.com

Common Myths about Telematics

In a world where it’s difficult to distinguish between fake news and real news, telematics or black-box insurance is struggling to gain confidence of car owners and fleet managers.

I have earlier discussed at length what Telematics is and what the various benefits and drawbacks of going for telematics insurance are.Today I’ll try to bust some common myths prevailing about this new age motor insurance policy.

Telematics was touted as next big thing in Insurance business and was considered to be a win-win solution for insurers and customers. A device which cuts down your insurance premium and rewards you for good driving habits!! Isn’t it the best solution for customers trying to save their hard earned money?? Well it is, but having a black box fitted into your vehicle, monitoring your driving history creeps out even the most optimistic of the lot.

Telematics Myth 1: Black-Box data will be utilised to penalise drivers who have crossed their stated mileage

The basic concept of Pay-as- you-drive insurance is to ensure that if you drive approximately up to your pre-stated mileage you will not be required to pay extra premium and if you drive more than that an extra top-up would be required to be paid. But if your telematics reads more mileage then you can always get it amended. Telematics is especially rewarding for customers who like to use their secondary vehicle for few getaways each year.

Telematics Myth 2: Insurers may randomly charge customers who drive rash or over speed

A good driving pattern over a period of time can be rewarding under this scheme but over speeding occasionally or hard braking won’t affect your premium. Telematics is there to reward good behaviour and not to penalise randomly.

Telematics Myth 3: It will be used as surveillance device to track customer’s location

I know this is the biggest concern for every customer thinking of getting a black box fit in their vehicles, but unlike the common perception insurers don’t want to track where you are travelling. They want to understand how you drive so that they can offer you the best possible discounts of motor premium quotes.

Telematics Myth 4: It will relay the data to police in case over speeding is observed

Yes this is another of the concerns but in reality nothing of this sort is done nor is the telematics device configured to relay such signals. Telematics data can be used by law enforcing agencies only in case of serious incident or an official enquiry is made.

Telematics Myth 5: Telematics device will mess with your car machinery and will affect your car’s warranty

The device doesn’t mess with internal machinery of your vehicle, in-fact it is plugged-into your vehicle’s diagnostic port. Car’s warranty is also not affected by installing this device.

I hope I have been able to address the misconceptions about the blackbox insurance. Read More about 10 Factors That Affect Your Car Insurance Premium

Copyright © ianswer4u.com