[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12452
  • Last Modified:

oops Concepts in Java

How do you define Abstraction & Encapsulation ??

Please explain me with an example
0
yrrm
Asked:
yrrm
1 Solution
 
mbormannCommented:
check out http://iraf.noao.edu/iraf/web/ADASS/adass_proc/adass_95/cogginsj/cogginsj.html

http://www.cs.olemiss.edu/~hcc/csci581/notes/dataAbstraction.html

They are one and the same in Java

Data Abstraction is the process of defining user types and using these types only through a set of interface operations instead of directly manipulating their representations.

definition of encapsulation from Rumbaugh's Object Oriented
 Modeling and Design, " Encapsulation (also called
 information hiding) consists of separating the external
 aspects of an object, from the internal implementation
 details of the object, which are hidden from other
 objects. Encapsulation prevents a program from
 becoming so interdependent that a small change has
 massive ripple effects. The implementation of an object
 can be changed without affecting the applications that
 use it. ...Encapsulation is not unique to object-oriented
 languages, but the ability to combine data structure and
 behavior in a single entity makes encapsulation cleaner
 and more powerful than in conventional languages that
 separate data structure and behavior."

All Object oriented languages have special language mechanisms to support data abstraction. In Java, an encapsulated type defines both state and behavior for that type. Such an encapsulated type is called a Class.

Class declarations include data and the methods that operate on the data. The data in an object is usually called its instance variables
or fields ,and the functions are called as methods in Java.

A specific object that is an instance of a class will have specific values for its fields that define its current state .

It cant be stressed enouygh that the key to making Encapsulation work is to write code that never 'directly' accesses instance variables or fields in a class .Programs should interact with this data only throught the so called accessor methods called as getXXX() and mutator methods caslled as setXXX()

Encapsulation is the key to give the object it's 'black box' behavior,wich is the key to reuse and reusability.This means that you may totally cxange the way how the object stores its data but as long as it continues to use the same methods to manipulate the data ,no other object will care.

As a example if in future Sun changes the String class ,to have Reference counting built in or any such major speedy modification,you wont even know abt it! This is the beauty of Data Abstraction /Encapsulation.

a short example
public class Address
 {
private String street;
//....

public void setStreet(String s)
{
//you can do checks here
this.street=s;
}
public String getStreet()
{
//you can do conditional checks here
 return this.street;
}
//....
//....

 };

 Java supports information hiding in it's classes through
 it's access control mechanisms. The specifiers private,protected
,public & no specifier (default is package,equivalent to C++'s friendly) control access to the members of the class from outside the class.

I used Core Java 2 vol 1 for reference.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now