aspose file tools*
The moose likes Beginning Java and the fly likes Encapsulation and abstraction confusing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Encapsulation and abstraction confusing?" Watch "Encapsulation and abstraction confusing?" New topic
Author

Encapsulation and abstraction confusing?

raghunath venkat
Greenhorn

Joined: Aug 12, 2009
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 09, 2011
Posts: 5
They asked you that in a Job interview?
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
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

Joined: Jun 15, 2011
Posts: 87

Click on below link:

Coderanch encapsulation and abstraction

This may clear you doubts.


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
I take your point, Mike Okri
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

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)


Please correct my English.
Mike Okri
Ranch Hand

Joined: Jun 22, 2011
Posts: 83
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

Joined: Oct 31, 2011
Posts: 13
abstraction in java is an idea( i mean a principle or a theory) and encapsulation is an efficient implementation of abstraction
Kalidas Badiger
Greenhorn

Joined: Nov 03, 2011
Posts: 1


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

Joined: Oct 13, 2005
Posts: 38865
    
  23
Kalidas Badiger welcome to the Ranch
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Vish Shukla
Ranch Hand

Joined: Oct 12, 2008
Posts: 111
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.


Thanks & Regards,
Vishal S Shukla (SCJP 93%, SCWCD 94%, SCBCD 100%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulation and abstraction confusing?