Monday, February 9, 2009

Encapsulation

Encapsulation Advantages:
Security
Easy to enhance
Maintainability
Modularity
Example:
class Sample
{
public int i=10;
public int getId()
{
return i;
}
public void setId(int x)
{
this.i=x;
}
}
The major limitations od encapsulation is,it increases the code (because we have to getter and setter methods for the data variables )and hence slows down the execution.

Tightly encapsulated class:
A class is said to be tightly encapsulated if and only if data members as the private
Check whether the fallowing classes are tightly encapsulated or not.
a)
class A
{
private int x=10;
public void setX(int x)
{
this.x=x;
}
public int getX()
{
return x;
} ------------------//tightly encapsulated

b). class A{ private int y=10;} -----//tightly encapsulated

c). class A{ private int x=20;} ----// A is tightly encapsulated, B is not
class B extends A{ private int y=30;}

d). class A{ int y=20;}
class B extends A{ private int z=40;}
class C extends B { private int l=50; }

• If the parent class is not tightly encapsulated no child class is tightly encapsulated.




0 comments:

Post a Comment