oops Concepts in Java

How do you define Abstraction & Encapsulation ??

Please explain me with an example
yrrmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.