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

Abstraction

in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
hey,
i am unable to understand the practical implementation of Abstraction in Java. Will you please explain and implement Abstraction with a suitable and simple example.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

List (or even Collection): abstraction.
ArrayList: implementation.
in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
Sorry,
but I am not able to get it.
Please explain the answer.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The java.util.List interface class contains a bunch of methods. Those methods define the list abstraction: lists can have elements added, be cleared, be iterated over, etc.

The java.util.ArrayList class is a concrete implementation of the list abstraction.
Bimal Sahay
Greenhorn

Joined: Aug 14, 2009
Posts: 4
Just imagine interface as something you can see and Implementation as something you cannot see. So when you press accelerator of your car, you would never know how speed of car increases but it does. If you create a Accelerator interface, it can define you the template. Something like, to be an accelerator, you need to implement these set of methods. Now the implementation depends on which car manufacturer designs it. May be slight different way of doing for different car manufacturers.

To elaborate on davids example, List defines a template(sets up the essentials) to be able to hold objects as a list. ArrayList implements List to announce that it follows the template to be a list and it has its own way of doing it that you dont need to worry about. So List is an abstraction to Arraylist implementation.

In terms of Java code, even data hiding is abstraction. Like you make you instance variables as private so that only methods inside that class can change it and you provide to the world, a setter and getter method to change instance variable values. You can write your own rules inside these methods and the world has to follow it.

There are many places in Java where you would use abstraction.

Biggest advantage: I dont change my accelerator interface, but impliment new way of accelerating. Still the world see it as a peddle that can be pressed and speed increases.

This is my understanding. Please correct me if you think different.


Bimal
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Cross posted
http://www.java-forums.org/new-java/28838-abstraction.html


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
Darryl Burke wrote:Cross posted
Please beware of posting twice. And welcome to the Ranch
in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
Campbell Ritchie wrote:
Darryl Burke wrote:Cross posted
Please beware of posting twice. And welcome to the Ranch


Sorry,
as i am a new user to the forum, so i didn't knew the rules of the forum.Now i will keep this in mind.
thanks for all the users for their informative answers..
in.anurag Singh
Greenhorn

Joined: May 14, 2010
Posts: 25
after searching more about this topic in this forum and other websites..i am writing my mind about abstraction here..
Definition: Abstraction simply means hiding non-essentianl details.
Implementation: The most simple example of Abstraction is .equals(object o). this function is provided to match two objects in java. we don't know or needn't know how the method is coded by the java team, but it provided us that function and we have to simply use it.

Am i right, if wrong, please correct me..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
I would say that abstraction means hiding as many details as possible. Expose only what you really have to. Also it means reducing something to its bare essentials. Like a car. You have a wheel; turn it right and the car goes right. All cars have a similar steering wheel and it does the same thing. But we don't necessarily know how it works.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstraction