my dog learned polymorphism
The moose likes Beginning Java and the fly likes Doubts in Basic Concepts of OOPS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubts in Basic Concepts of OOPS" Watch "Doubts in Basic Concepts of OOPS" New topic

Doubts in Basic Concepts of OOPS

Mark Tigerwood

Joined: Nov 04, 2006
Posts: 11
Hi all,

This is very very basic priciple in OOPS, which I didn't have clear understanding.. Though I have searched in google and find the definition, it's not clear and I want some good example to explain it. They are:
1. Abstraction
2. Encapsulation

Above mentioned two are hiding something correct? How to differentiate the above thing?

Before that, I will tell my understanding about both: Encapsulation is Data Hiding. Example: Declaring the variables private, so that other classes can't able to access the variables directly, and to access thru public gette/setter. Is it Correct?

What about abstraction? Anything related with abstract keyword in java? Like abstract classes can't have any implementation. So we are hiding the implementation.

Can I say like this:
Encapsulation: Hiding the variables/methods in a class
Abstraction : Hiding the Implementation of a method.

And also for knowing some basic concepts like this in Java, is there any liks?
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
Abstraction: In object-oriented programming we are trying to simulate the real world closely. Data abstraction entails recording certain characteristics of an object from the real world. Only the essential characteristics are used to create a class or a type. All trees will have leaves and branches. These characteristics are common to all trees. Only these common characteristics will be recorded when we create an abstract type called tree. Specific characteristics of specific trees will not be recorded here.

Your idea about encapsulation is correct.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't think I agree with the "real world" aspects of that answer, unless you are truly writing a simulation and even then it might be an unreal world. Like DOOM. Close mapping to "real" things was a hype point in early OO but it's not one of the important things to me any more.

That aside, one definition of abstraction I like is ignoring certain parts of a problem that aren't important so you can more clearly see the parts that are important. In Java I might use the List interface:

I'm focused on the List-ness of the argument. I can put things in, get them out, remove them, etc. I'm ignoring whether it's a LinkedList, ArrayList or some new fangled List that you just invented. I don't want my code to talk about those details now, I just want it to talk about my reasons for putting things in and so on. (Note: I am declaring that I'm not interested in the relative performance of get by index or append when I use this abstraction!)

In that example we have an abstract type List and some concrete type that we don't care about. We can also talk about abstraction in lines of code. If you see a comment in code "The next 5 lines gnarfle the garthak" it's a good sign that those 5 lines should be in another method, maybe called gnarfle(garthak). Now that method is an abstraction because when I read the original method I can ignore just how the garthak gets gnarfled. Oh, and garthak.gnarfle() might be even better.

(Hmmm, checking spelling I find that "gnarfle the garthak" has a rude meaning with you hip kids today. Makes this whole example read a little differently.)

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Hi to all.
The concept of abstraction as given in Khalid A. Mughal's book is that abstraction is a way in which humans handle complexity. It defines the attributes & properties (state) and behaviours of an object that differentiate it from other objects.
A class models abstraction by defining the properties and behaviours for the objects; it acts as a template or blueprint for creating objects.
An object is a realization of the abstraction & has the same properties & behaviours as defined by its class.

Hope this helps to understand the concept of abstraction.

SCJP 5.0 (100%)
I agree. Here's the link:
subject: Doubts in Basic Concepts of OOPS
It's not a secret anymore!