answersLogoWhite

0


Best Answer

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.

If you want maintainability, flexibility, and extensibility (and I guess, you do), your design must include encapsulation. How do you do that?

• Keep instance variables protected (with an access modifier, mostly private).

• Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.

• For the methods, use the JavaBeans naming convention of set and get.

We call the access methods getters and setters although some prefer the fancier terms accessors and mutators. (Personally, I will be using the terms getters and setters) Regardless of what you want to call them, they're methods that other programmers must go through in order to access your instance variables. They look simple, and you've probably been using them forever if you have been writing java code:

public class GoodExample {

// protect the instance variable only an instance of your class can access it

private int size;

// Provide public getters and setters

public int getSize() {

return size;

}

public void setSize(int newSize) {

size = newSize;

}

}

You are now probably mumbling, what benefit is it to have methods that do nothing and just set or get values. I would rather have a public variable. If you did that go to the first paragraph under Encapsulation and re-read the whole thing. And if you did not do that but are asking me, where is the validation code that we are supposed to have in the setSize() method to ensure no one modifies it to invalid values, my friend this is just an example class. I leave you to ponder about how the implementation needs to be. Atleast you wont end up on the receiving side of unexpected shocks because of someone like me coding along with you or worse !!!

User Avatar

Wiki User

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

Wiki User

13y ago

It is the basis of OOP. Without it, it will not be OOP. Do you want any one else to manipulate your bank account (you bank account being encapsulated within you if only you can access it)?

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why encapsulation important for object-oriented programming?
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.


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, 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.


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 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.

Related questions

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.


3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.


What are components of object oriented programming?

abstraction,encapsulation,inheritence,polymorphism,object


What are the various elements of Object oriented programming?

Encapsulation, data hiding, inheritance and polymorphism.


What are the basic concept of object oriented programming language?

Inheritance Encapsulation Polymorphism Abstraction


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 are the features of object oriented program?

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


Some important features of java programming?

The important features of Java are the ones that relate to the object oriented concepts like: a. Inheritance b. Polymorphism c. Encapsulation d. Data Hiding e. Data Abstraction etc


What are the four pillars object oriented programming?

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


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.


What is an object in programming terminology?

programming objects are used to store variables (fields) which can be accessed or manipulated through the use of methods or functions. Objects are used as a means of data encapsulation.


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.