Results 1 to 5 of 5
  1. #1
    sandeepsai17 is offline Member
    Join Date
    Jun 2009
    Posts
    30
    Rep Power
    0

    Default 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 11:55 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do the books/tutorials say?

  3. #3
    sandeepsai17 is offline Member
    Join Date
    Jun 2009
    Posts
    30
    Rep Power
    0

    Default 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 Example
    Last edited by sandeepsai17; 05-21-2010 at 12:02 PM.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by sandeepsai17 View Post
    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 Example

    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 02:04 PM.

  5. #5
    sandeepsai17 is offline Member
    Join Date
    Jun 2009
    Posts
    30
    Rep Power
    0

    Default

    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;           
    	}
    }
    that means i did some mathematical calculations to get z_location in getter method [ just Example not meaningful] .If i write in this manner is it full fill the Encapsulation concept

Similar Threads

  1. Abstraction
    By anurag in forum New To Java
    Replies: 6
    Last Post: 05-15-2010, 06:15 PM
  2. diff b/w jsf and visual web jsf
    By kattavijay in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 03-16-2009, 11:44 AM
  3. [SOLVED] Abstraction: Pillar of OOP?
    By leiferouis in forum New To Java
    Replies: 7
    Last Post: 01-31-2009, 08:31 PM
  4. JMeld 1.9 (Visual diff and merge)
    By Kees Kuip in forum Java Software
    Replies: 0
    Last Post: 03-08-2008, 10:18 AM
  5. diff between WML d XHTML
    By bakiya in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 11-02-2007, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •