Results 1 to 5 of 5
- 05-21-2010, 11:48 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 30
- Rep Power
- 0
diff between abstraction and Encapsulation
hi to all,
i am very new to java .I want exact definitions for abstraction ,inheritance .what is the diff between abstraction and Encapsulation.What is information how it it is different from above two concepts and it can be achived.Last edited by sandeepsai17; 05-21-2010 at 12:55 PM.
- 05-21-2010, 11:52 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
What do the books/tutorials say?
- 05-21-2010, 12:54 PM #3
Member
- Join Date
- Jun 2009
- Posts
- 30
- Rep Power
- 0
diff between abstraction and Encapsulation
Sorry ,I asked question wrongly .Instead of Encapsulation i wrote inheritance.
i am little bit confusion with this concepts.Actually I read java world tutorial , i understood like below.
Abstraction is a technique to choosing common features of a objects or concepts.It also the
provides hiding of data or information which is not necessary to us and provide a way to access data which is necessary to us.
I don't Know it is right or not i understand like this small example .
class Student{
int age ;
String name;
int rollid;
String batch;
// the above common features of a student,the next fields related to address
String addressLine1;
String addressLine2;
String houseNo;
String Line No ;
String City;
String District;
String State ;
String pin ;
//etc....
//all above line from first comment line are related to Address.It is difficult to access and //specify all these common to address.
//some more methods and declarations
}
I understood that it is better to separate Address related data in a separate class like below
class Address{
String addressLine1;
String addressLine2;
String houseNo;
String Line No ;
String City;
String District;
String State ;
String pin ;
// methods related to data
}
and i modified my Student class like this
class Student{
int age ;
String name;
int rollid;
String batch;
Address address ;
}
now i can specify Address data using Address instance.It is hiding data which not necessary to us like Address related data ,and Showing Address class instances .From Address instances i can access address related data.
I hope i am little bit right about abstraction .But coming to In Encapsulation . Several times i hear that Encapsulation hiding the data behind methods . I am not satisfied with this definition . I want definition with small ExampleLast edited by sandeepsai17; 05-21-2010 at 01:02 PM.
- 05-21-2010, 03:01 PM #4
when you define member variables without a access modifier they are accessible directly, which contradict the idea of encapsulation. in order to be conform with encapsulation you should declare your member variables with the access modifier private and then provide setters to change it. usually the member variables are initialized through a constructor and the setters are used for later changes. example:
Java Code:class Address { private String addressLine1; private String addressLine2; private String houseNo; private String LineNo; private String City; private String District; private String State; private String pin; // the constructor, feel free to add more arguments public Address(String line1, String line2, String City) { this.addressLine1 = line1; this.addressLine2 = line2; this.City = City; } public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getHouseNo() { return houseNo; } public void setHouseNo(String houseNo) { this.houseNo = houseNo; } public String getLineNo() { return LineNo; } public void setLineNo(String lineNo) { LineNo = lineNo; } public String getCity() { return City; } public void setCity(String city) { City = city; } public String getDistrict() { return District; } public void setDistrict(String district) { District = district; } public String getState() { return State; } public void setState(String state) { State = state; } public String getPin() { return pin; } public void setPin(String pin) { this.pin = pin; } }
so now the variables are private and hidden outside the class and if you change the name of a variable in your class other java code that extends your class will not be affected because the getters and setters will not change.Last edited by j2me64; 05-21-2010 at 03:04 PM.
- 05-24-2010, 07:04 AM #5
Member
- Join Date
- Jun 2009
- Posts
- 30
- Rep Power
- 0
that means only use private variables and use getters and setters for accessing the variables is all about Encapsulation.I read the another definition for encapsulation as below,
Encapsulation is a language construct that facilitates the bundling of data with the methods operating on that data . The author said one main rule i.e
Rule1:
Place data and the operations that perform on that data in the same class.
for example i understand like this , if i have class like below
Java Code:class Example { private int x_location; private int y_location; private in z_location; public int getX_location() { return x_location; } public void setX_location(int x_location) { this.x_location = x_location; } public int getY_location() { return y_location; } public void setY_location(int y_location) { this.y_location = y_location; } public int getZ_location() { // do some mathematical operations to get z location based on x //and y location for example an assign it to z_location ; return z_location; } public void setZ_location(int z_location) { this.z_location = z_location; } }
Similar Threads
-
Abstraction
By anurag in forum New To JavaReplies: 6Last Post: 05-15-2010, 07:15 PM -
diff b/w jsf and visual web jsf
By kattavijay in forum JavaServer Faces (JSF)Replies: 0Last Post: 03-16-2009, 12:44 PM -
[SOLVED] Abstraction: Pillar of OOP?
By leiferouis in forum New To JavaReplies: 7Last Post: 01-31-2009, 09:31 PM -
JMeld 1.9 (Visual diff and merge)
By Kees Kuip in forum Java SoftwareReplies: 0Last Post: 03-08-2008, 11:18 AM -
diff between WML d XHTML
By bakiya in forum Sun Java Wireless ToolkitReplies: 0Last Post: 11-02-2007, 12:26 PM
Bookmarks