• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encapsulation & Information Hiding and Inheritence  RSS feed

 
Muhammad Ahsan Jamshaid
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone clearly describe the difference b/w Encapsulation,Information Hiding and Inheritance... As i m mixing them up....
--Jamshaid
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Information hiding is all about exposing your privates. It's not polite to expose your privates, so we don't do it in Java.

Data is important, so a class doesn't like other classes to see its data. So, to hide data, a class makes instance variables private. We don't expose our privates in Java. This is information hiding.

Now, sometimes people need to get at that data. We allow people to get to our privates by making public methods that control how our privates are accessed. A public getter will allow others to get at our privates. A public setter will allow others to manipulate our privates. If you have a public getter, the method is 'readable'. A setter makes it 'writable'.

Making instance variables private, and federating access through public setter and getters is the essence of encapsulation of data in Java.

Inhiritance is quite a bit different. Once class can inherit from another class: a rectangle is-a shape, and a square is-a rectangle. We could express this in code by creating three classes, Shape, Rectangle and Square, and have Rectangle inherit from Shape, and Square inherit from Rectangle.

Inheritance is very kewl.

Clear as mud? Good.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you Google for definitions of Encapsulation you will find many variations. I'd expect all to include something along the Information Hiding lines. Some will also talk about combining data and code into one thing and some aspects of cohesion. So, yes, Encapsulation and Information Hiding are related but not identical.

I'll leave you to hunt down Inheritance on your own. Try Google, try WikiPedia. Let us know what you think.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!