aspose file tools*
The moose likes Java in General and the fly likes Encapsulation and Abstraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encapsulation and Abstraction" Watch "Encapsulation and Abstraction" New topic
Author

Encapsulation and Abstraction

Eshan Kapoor
Ranch Hand

Joined: May 15, 2013
Posts: 37

Hi,
I have 3 questions. Please help me out. I googled them but due to mixed responses got really confused.

Q1) What is the difference between Encapsulation and Abstraction( Please explain with some examples. It would be a big help)

Q2) In java when we use access specifiers, do we implement data hiding or abstraction?

Q3)Does the following statement define abstraction or inheritance? Actually I am getting confused by the words "more abstract" used for superclass which I read in Head First Java. The statement is as follows -
Putting the common code in a superclass(more abstract) so that the subclasses(more specific) can inherit that code.

Thank You. I would appreciate your help.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Welcome to the Ranch.

You will learn more if you tell us what you think the answers are and why and we will correct you if you are wrong.
Eshan Kapoor
Ranch Hand

Joined: May 15, 2013
Posts: 37

Hi Tony,

Okay I'll give it a try.

Ans1)
Using Encapsulation, we don't let any code directly access the the instance variables by marking them private. Rather, we let the getters and setters do that. Hiding implementation details behind a public programming interface - key benefit of encapsulation. Similarly, we put everything in an object i.e., every object has its own copy of instance variables. So no other object can change those values. So essentially, Encapsulation means wrapping up of data in a single unit.

Talking about Abstraction - I have always read that it tells something has to be done instead of how it has to be done. Say we drive a car. We know about changing gears, applying brakes and accelerator. But we don't how the car stops when we apply brake or how the gear changes. Abstraction only tells that ok this is gonna happen but we are unaware about how it is gonna happen. (Trust me I understand this theoretically but I don't understand how should I apply it while programming)

Ans2) By the use of access specifiers we limit the visibility of a particular member. So may be data hiding. (I have a feeling that I'm wrong)

Ans3) Using inheritance, subclasses can have the instance variables and methods from their parent class. So this sentence may define inheritance. But the words "more abstract" for superclass makes me mad here. How are we having abstraction here? Is it that the words "more abstract" tell us that this class has an abstract method which has no meaning by itself unless implemented in a subclass? for example if we talk about class Shape(say a concrete class) with a method area(). Its subclasses are Circle, Square and Hyperbola. So the code inside Shape's area method will have no meaning since a shape can be anything. So calling area() on Shape's object would be meaningless unless we define the Shape class and area() as abstract and let the subclasses implement that method according to their shape...each having different implementation.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Is it that the words "more abstract" tell us that this class has an abstract method which has no meaning by itself unless implemented in a subclass?

No, it is saying the superclass class is more generalised (less specific) than the subclass.
For example putting the classes Feline, Mammal, Lion, Animal into order starting with the most abstract and ending with the most specific we get Animal, Mammal, Feline, Lion. In a class hierarchy Animal is the most abstract and so would be the top level class extended by the next most abstract ie Mammal which would be extended by Feline and finally by the most specific which is Lion.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Eshan Kapoor wrote:Okay I'll give it a try.

Ans1)
Using Encapsulation, we don't let any code directly access the the instance variables by marking them private.

Actually, I've always thought of encapsulation as "putting relevant stuff in the same place" (in Java, a class) - ie, rather like its meaning in English.

The "private" stuff has more to do with data (or actually information) hiding - also important, but not quite the same.

Ans2) By the use of access specifiers we limit the visibility of a particular member. So may be data hiding. (I have a feeling that I'm wrong)

Nope. Sounds good to me.

Ans3) Using inheritance, subclasses can have the instance variables and methods from their parent class. So this sentence may define inheritance. But the words "more abstract" for superclass makes me mad here. How are we having abstraction here?

Simply because it's so. A superclass is more abstract than a subclass, just as a Shape is more abstract than a Square (a Square is a Shape; but not every Shape is a Square).

So, look at the question:
"Putting the common code in a superclass(more abstract) so that the subclasses(more specific) can inherit that code."
What it it describing? Abstraction or Inheritance?

Winston

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

Joined: May 15, 2013
Posts: 37

Thanks Tony. Now I got the point . And what do you think about my other 2 answers?
Eshan Kapoor
Ranch Hand

Joined: May 15, 2013
Posts: 37

Hi Winston,

So, look at the question:
"Putting the common code in a superclass(more abstract) so that the subclasses(more specific) can inherit that code."
What it it describing? Abstraction or Inheritance?


It is describing inheritance.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulation and Abstraction