Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, October 16, 2016

Simple way to understand Encapsulation and Abstraction

Simple way to understand Encapsulation and Abstraction


Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth.

Checked out the below already

Abstraction VS Information Hiding VS Encapsulation

difference between abstraction and encapsulation?

I found very hard to understand those concepts with out a real and simple example class/code snippet.

One of my colleagues said abstraction is nothing but creating abstract class and normal class that protects its member variable with scope is called Encapsulation.

Is there a simple way I can understand and help others to understand what exactly they are, rather than repeating the below?

Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics.

Answer by Cratylus for Simple way to understand Encapsulation and Abstraction


Abstraction is the process where you "throw-away" unnecessary details from an entity you plan to capture/represent in your design and keep only the properties of the entity that are relevant to your domain.
Example: to represent car you would keep e.g. the model and price, current location and current speed and ignore color and number of seats etc.

Encapsulation is the "binding" of the properties and the operations that manipulate them in a single unit of abstraction (namely a class).
So the car would have accelarate stop that manipulate location and current speed etc.

Answer by Jai for Simple way to understand Encapsulation and Abstraction


Abstraction is hiding the information or providing only necessary details to the client.

e.g Car Brakes- You just know that pressing the pedals will stop the vehicle but you don't need to know how it works internally.

Advantage of Abstraction Tomorrow if brake implementation changes from drum brake to disk brake, as a client, you don't need to change(i.e your code will not change)

Encapsulation is binding the data and behaviors together in a single unit. Also it is a language mechanism for restricting access to some components(this can be achieved by access modifiers like private,protected etc.)

For e.g. Class has attributes(i.e data) and behaviors (i.e methods that operate on that data)

Answer by AmitG for Simple way to understand Encapsulation and Abstraction


public abstract class Draw {      public abstract void drawShape(); // this is abstraction.  Implementation detail need not to be known.      // so we are providing only necessary detail by giving drawShape(); No implementation. Subclass will give detail.          private int type;    // this variable cannot be set outside of the class. Because it is private.      // Binding private instance variable with public setter/getter method is encapsulation         public int getType() {           return type;      }        public void setType(int type) {  // this is encapsulation. Protecting any value to be set.          if (type >= 0 && type <= 3) {              this.type = type;          } else {              System.out.println("We have four types only. Enter value between 0 to 4");              try {                  throw new MyInvalidValueSetException();              } catch (MyInvalidValueSetException e) {                  e.printStackTrace();              }            }      }  }  

Abstraction is related with methods where implementation detail is not known which is a kind of implementation hiding.
Encapsulation is related with instance variable binding with method, a kind of data hiding.

Answer by MikeSW for Simple way to understand Encapsulation and Abstraction


An example using C#

//abstraction - exposing only the relevant behavior  public interface IMakeFire  {       void LightFire();  }    //encapsulation - hiding things that the rest of the world doesn't need to see  public class Caveman: IMakeFire  {       //exposed information         public string Name {get;set;}         // exposed but unchangeable information       public byte Age {get; private set;}         //internal i.e hidden object detail. This can be changed freely, the outside world       // doesn't know about it       private bool CanMakeFire()       {             return Age >7;       }         //implementation of a relevant feature       public void LightFire()       {          if (!CanMakeFire())          {             throw new UnableToLightFireException("Too young");          }          GatherWood();          GetFireStone();          //light the fire         }         private GatherWood() {};       private GetFireStone();  }    public class PersonWithMatch:IMakeFire  {        //implementation   }  

Any caveman can make a fire, because it implements the IMakeFire 'feature'. Having a group of fire makers (List) this means that both Caveman and PersonWithMatch are valid choises.

This means that

  //this method (and class) isn't coupled to a Caveman or a PersonWithMatch    // it can work with ANY object implementing IMakeFire    public void FireStarter(IMakeFire starter)    {          starter.LightFire();      }  

So you can have lots of implementors with plenty of details (properties) and behavior(methods), but in this scenario what matters is their ability to make fire. This is abstraction.

Since making a fire requires some steps (GetWood etc), these are hidden from the view as they are an internal concern of the class. The caveman has many other public behaviors which can be called by the outside world. But some details will be always hidden because are related to internal working. They're private and exist only for the object, they are never exposed. This is encapsulation

Answer by anuja dhakite for Simple way to understand Encapsulation and Abstraction


data abstraction: accessing data members and member functions of any class is simply called data abstraction.....

encapsulation: binding variables and functions or 1 can say data members or member functions all together in a single unit is called as data encapsulation....

Answer by Yasser for Simple way to understand Encapsulation and Abstraction


Abstraction is a process where you show only ?relevant? data and ?hide? unnecessary details of an object from the user. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.

Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.

enter image description here

Source:

Answer by Rajesh Maurya for Simple way to understand Encapsulation and Abstraction


Data Abstraction: DA simply filtering the concrete item. By the class we can achieve the pure abstraction, because before creating the class we can think about only concerned information about class.

Encapsulation: It is mechanism, by which we protect our data form outside.

Answer by Premraj for Simple way to understand Encapsulation and Abstraction


Abstraction is generalised term. i.e. Encapsulation is subset of Abstraction.

Abstraction is a powerful methodology to manage complex systems. Abstraction is managed by well-defined objects and their hierarchical classification.

For example a car in itself is a well-defined object, which is composed of several other smaller objects like a gearing system, steering mechanism, engine, which are again have their own subsystems. But for humans car is a one single object, which can be managed by the help of its subsystems, even if their inner details are unknown. Courtesy


Encapsulation: Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

class Bag{      book;      pen;      ReadBook();  }  

Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.

So, when you access the property you can validate the data and set it. Courtesy

Answer by Jayraj Srikriti Naidu for Simple way to understand Encapsulation and Abstraction


Encapsulation can be thought of as wrapping paper used to bind data and function together as a single unit which protects it from all kinds of external dirt (I mean external functions).

Abstraction involves absence of details and the use of a simple interface to control a complex system.

For example we can light a bulb by the pressing of a button without worrying about the underlying electrical engineering (Abstraction) .

However u cannot light the bulb in any other way. (Encapsulation)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.