Thursday, 13 June 2024

Abstraction vs Encapsulation

 

This is very basic and important question asked in many interviews. This question is basically asked to check if candidate have a good understanding of OOP's (Object Oriented Programming) concepts or not. 

 

Encapsulation

Abstraction

Complexity Hiding.

Showing only what is necessary.

Encapsulated members Direct access and modification not allowed from other class because encapsulated members are private.

Abstracted Members Directly accessible, because abstract members are only public.

Encapsulated members even not visible outside and can’t see weather exists are not.

Abstracted members are visible outside, but can’t see how it is implemented.

Encapsulation binds all members into single unit using class and access specifiers Private, protected, internal etc.

Abstract is layer or interface over the basic implementation methods.

Abstraction can achieve using Interface and abstract class.

Solves problems in implementation level.

Solves Problem in Design Level.

Information hiding

Implementation hiding.

Encapsulation is Inner Layout used in terms of implementation

Example : Inner implementation of mobile phone , how keypad button, display screen connect each other using circuits.

Abstraction – is outer layout used in terms of design.

Example : outer look of mobile phone like it has display screen, keypad button and dialing

Whereas the objects that result in encapsulation need not be abstracted.

The objects that help to perform abstraction are encapsulated.

Encapsulation is closely related to abstraction, as abstracted methods help to give access to encapsulated data in secure manner, because encapsulated data direct access restricted.

Abstraction works together with encapsulation, as encapsulation hide implementation.

 

Abstraction lets you focus on what the object does while Encapsulation means how an object works

 

In summary, encapsulation emphasizes data hiding and access control within an object, while abstraction emphasizes creating a simplified view of a complex system by hiding implementation details. Both principles promote modularity, maintainability, and code reusability in object-oriented programming.

 

 

 


 

Abstraction vs. Encapsulation

This is very basic and important question asked in many Java interviews. This question is basically asked to check if candidate have a good understanding of OOP's (Object Oriented Programming) concepts or not. 

 

Abstraction - In very simple words Abstraction is concept of hiding complexity of a system and showing only necessary details to its user. So user can access the necessary functionalities easily without thinking of internal complexity of the system. Abstraction mainly focusses on outside view or design of the system.

 

In real word scenarios we can take an example of a mobile phone. In mobile we as a user only see's the necessary details or features like screen, buttons etc. and all other complexities (circuits, chips and internal wiring etc) are hidden from us.

 

In  programming abstraction is implemented through interfaces and abstract classes.

 

Encapsulation - Encapsulation is a process of binding members (attributes) and functions (methods) in a single entity, so that they can be protected from outsiders. Encapsulation mainly focus on internal design of a system.

 

In real word scenarios we can take the same mobile phone example, where after binding all attributes and functionality we call it a mobile phone. And after encapsulating everything in a single entity it is safe from outsiders.

 

In programming encapsulation is implemented through class and access modifiers. We encapsulates attributes and methods in a single entity which is called class and we can use access modifiers (public, private, protected and default) to provide different protection levels to internal attributes, methods or the class itself.

 

Abstraction vs Encapsulation n Telugu




No comments:

Post a Comment

Thanks for the contribution, our team will check and reply back if response required.