• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation and abstraction confusing?

 
raghunath venkat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encapsulation is having private data and those protected and accessible by setters and getters.
Is this only definition in java ? or any other definition is there?

For abstraction, it hides the implementation means using abstract keyword in java or any other meaning is there?

Please clarify me.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50184
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the abstract keyword has nothing to do with data abstraction. Those questions are too large to answer quickly; I suggest you start with Encapsulation_%28object-oriented_programming%29 and Abstraction_(computer_science) on Wikipedia. The encapsulation article is easier to understand. Then tell us what you still don’t understand.
 
Pancajanya Draj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They asked you that in a Job interview?
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, the abstract keyword has nothing to do with data abstraction

I think that the abstract keyword has something to do with data abstraction. It is used to define an abstraction as opposed to a concrete implementation. An abstract interface or class is the base from which a concrete implementation can be derived through the extends or implements keywords.
 
Aniket S. Kulkarni
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click on below link:

Coderanch encapsulation and abstraction

This may clear you doubts.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50184
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take your point, Mike Okri
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Okri wrote:
Campbell Ritchie wrote:No, the abstract keyword has nothing to do with data abstraction

I think that the abstract keyword has something to do with data abstraction. It is used to define an abstraction as opposed to a concrete implementation. An abstract interface or class is the base from which a concrete implementation can be derived through the extends or implements keywords.


The abstract keyword only means that this class has abstract methods and that this methods are not implemented in this class and needs to be implemented elsewhere.
An inteface is per definition abstract because all of its methods are.
An abstract class may be implemented without being extended. (anonymous inner classes)
 
Mike Okri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Petermann wrote:The abstract keyword only means that this class has abstract methods and that this methods are not implemented in this class and needs to be implemented elsewhere.

That's right. The abstract keyword is used to indicate that an interface or class has one or more abstract methods; thus indicating that the interface or class is an abstraction i.e. an incomplete representation of a system in which certain details are deliberately omitted.


 
poorvika chanda
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstraction in java is an idea( i mean a principle or a theory) and encapsulation is an efficient implementation of abstraction
 
Kalidas Badiger
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Abstraction refers to the act of representing essential features without including the background details or explanations but encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate and also prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.



 
Campbell Ritchie
Sheriff
Pie
Posts: 50184
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalidas Badiger welcome to the Ranch
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghunath venkat wrote:For abstraction, it hides the implementation means using abstract keyword in java or any other meaning is there?

The best way I know to explain abstraction is that it separates the 'what' from the 'how'.

When you learn to drive a car, you don't need to understand exactly how a car works - in fact thinking about it would probably get in the way of what you should be doing, which is concentrating on the road - all you need to know is what the pedals and gear lever do, and what you need to do to turn it on and off.

The same is true of program design; all that worrying about nuts and bolts gets in the way of building programs. If I want to use a List, all I should need to know is what it does, not how it does it.

Furthermore, since the 'what' says absolutely nothing about the 'how', designers are free to implement a List any way they see fit (and there are at least 5 well-used implementations in the standard Java libraries alone).

Encapsulation (or data hiding) is what helps designers maintain that separation. Not only do you not need to know how a class works, you can't find out; because it's hidden from you.

Of course, most of the standard classes are open-source now, so if you really want to beat yourself up you can find out, but ArrayList alone is over a thousand lines of code (and built on top of two abstract classes). Personally, I've got better things to do with my time.

HIH

Winston
 
Vish Shukla
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Winston mentioned, what Abstraction enables you to do is, the capability to use the same concrete class in completely unrelated clients. In other words, it allows client to see his perspective. Car is a concrete class. But if I am the vehicle tracking application, I am only concerned with its x & y co-ordinates - I can have an interface with only those methods. If I am a driver of the car, my abstraction will have methods like start, stop, accelerate and so on. Check out the article on abstraction at FearlessDeveloper.

Where as Encapsulation allows the class to completely control how it should behave - by controlling the way its data access & mutation. Car class may have data - speed. Client of car class cannot change speed of car from +100 to -100 directly. You need to change gears to get the speed down, break, take reverse and there will be limit on the max speed in reverse. This can be achieved using Encapsulation. You hide speed. You provide accelerator and break. Check out the article on Encapsulation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic