answersLogoWhite

0


Best Answer

Encapsulation example:

Ink is the important component in pen but it is hiding by some other material.

Abstraction example: ATM is a good example of abstraction, we doesn't know what are all the internal processes that are going when we access it..instead we know only the inquiries, deposit, withdrawl etc...

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Encapsulation in object-oriented programming languages describes the ability to restrict access to one or more members of a class and the means by which data and the methods that operate upon that data can be bound together within a class. All the functionality required in order to use an object of the class should be entirely self-contained -- encapsulated -- within the object itself.

Abstraction is the means by which we can interface with a group of derived classes through a common abstract data type (ADT) or abstract base class (ABC). ADTs cannot be instantiated in their own right, their sole purpose is to provide a common interface to all its derivatives, which must each provide the implementation for that common interface. This makes it possible to create collections of otherwise unrelated objects without the need to know the runtime type of those objects, provided we only access those objects through the common interface provided by the ADT.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Imagine that we both work for the same project and first you wrote the code for a class, and then I used your class in my program. Later on, you didn't like the way the class behaved, because some of its instance variables were being set (by me from my code) to values you hadn't anticipated. Their code brought out errors in your code. (Relax, I wont do that, dont worry.) Since, it is a Java program, so you should be able just to ship out a newer version of the class, which I could replace in my programs without changing any of my own code.

The above scenario highlights two of the promises or rather i should say benefits of Object Orientation (OO): flexibility and maintainability. But these benefits will not come automatically. You have to do something. You have to write your classes and code in a way that supports flexibility and maintainability. Just because Java supports OO concepts, it cannot write code for you. Can it?? For example, imagine if you made your class with public instance variables, and those other programmers were setting the instance variables directly, as the following code demonstrates:

public class BadExample {

public int size;

public int weight;

...

}

public class AnotherBadExample {

public static void main (String [] args) {

BadExample b = new BadExample ();

b.size = -5; // Legal but bad!!

}

}

Now go back the scenario we spoke about a paragraph ago. BadExample is your class and AnotherBadExample is my code. I have modified one of your variables in a way that it helps my code logic but that totally alters the way your class works. Now you are in trouble. How are you going to change your class in such a way that no one can alter your values directly (like what i have done in my code)? Your only choice is to write a method say setSize(int newVal) inside your class and then change the access modifier of the variable size to say, private. This will ensure that you handle instances when someone is trying to set a value to the size variable that you dont want and at the same time ensure that no one can access the size variable directly and mess with your code.

But, unfortunately, by doing that, you have broken my code. If I try to compile my AnotherBadExample class, i will get errors because the size variable is no longer visible for me.

How can we address this situation now? The best way is: not write such code where public variables are available for anyone and everyone to modify.

The ability to make changes in your code without breaking the code of all others who use your code is a key benefit of encapsulation. You should always hide implementation details. To elaborate, you must always have your variables as private and then have a set of public methods that others can use to access your variables. Since the methods are public anyone can access them, but since they are in your class you can ensure that the code works the way that is best for you. So in a situation that you want to alter your code, all you have to do is modify your methods. No one gets hurt because i am just using your method names in my code and the code inside your method doesnt bother me much

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Example of encapsulation and abstraction in an object oriented programming language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is encapsulation a characteristic of procedural or object oriented programming?

Encapsulation is one of the four pillars of object-oriented programming. The other three are inheritance, polymorphism and abstraction.


All object-oriented programming languages must have the 3 following features?

Abstraction, encapsulation and polymorphismare the three fundamental features of an object oriented programming language.


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


What is object based programming language?

Object-based programming language is a language that supports all the features of object oriented programming features like classes,object,encapsulation ,abstraction,polymorphism etc except inheritence.


Can abstraction encapsulation be achieved in C program if yes explain?

abstraction and encapsulation is one of the concepts of OOPs and C is not an OOP [Object Oriented Programming language] obviously abst & encap will not be supported by 'C' Abstraction & encapsulation is a concept of OOP [Object Oriented Programming] But, 'C' is not an OOP whereas it is a POP [Procedure oriented programming], so obviously 'C' does not support abstraction and encapsulation Answer Encapsulation is not inherently supported but it can be emulated in C. The use of static and extern keywords for functions are almost equivalent to your private and public keywords in java (encapsulation). Read up more on those keywords.. Structures become an object's attributes while functions accepting pointers the the said struct become its methods.

Related questions

What are the basic concept of object oriented programming language?

Inheritance Encapsulation Polymorphism Abstraction


Is encapsulation a characteristic of procedural or object oriented programming?

Encapsulation is one of the four pillars of object-oriented programming. The other three are inheritance, polymorphism and abstraction.


All object-oriented programming languages must have the 3 following features?

Abstraction, encapsulation and polymorphismare the three fundamental features of an object oriented programming language.


What are components of object oriented programming?

abstraction,encapsulation,inheritence,polymorphism,object


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


What are the features of object oriented program?

The features of object oriented programming are Abstraction, Encapsulation, Polymorphism & Inheritance


What is object based programming language?

Object-based programming language is a language that supports all the features of object oriented programming features like classes,object,encapsulation ,abstraction,polymorphism etc except inheritence.


What are the concepts of object oriented programming in c plus plus?

The concepts of OOP in C++ are the same as for OOP in any other programming language: abstraction, encapsulation, inheritance and polymorphism.


Can abstraction encapsulation be achieved in C program if yes explain?

abstraction and encapsulation is one of the concepts of OOPs and C is not an OOP [Object Oriented Programming language] obviously abst & encap will not be supported by 'C' Abstraction & encapsulation is a concept of OOP [Object Oriented Programming] But, 'C' is not an OOP whereas it is a POP [Procedure oriented programming], so obviously 'C' does not support abstraction and encapsulation Answer Encapsulation is not inherently supported but it can be emulated in C. The use of static and extern keywords for functions are almost equivalent to your private and public keywords in java (encapsulation). Read up more on those keywords.. Structures become an object's attributes while functions accepting pointers the the said struct become its methods.


What is the components of OOPS?

Java is an object oriented programming language. The various object oriented concepts in it are: * Class * Object * Instance * Method * Inheritance * Polymorphism * Abstraction * Encapsulation etc...


What is the distinct difference between object oriented concept and object oriented programming?

Object oriented concepts are a generalisation of the object oriented principals (encapsulation, inheritance, polymorphism and abstraction) without specifying a particular implementation of those principals. Object oriented programming is the application of those principals through an object oriented programming language.


What are the four pillars object oriented programming?

1.Abstraction 2.Encapsulation 3.Modularity 4.Hierarchy