
bring out the oop features in java with examples?

Encapsulation Example:
public class Box
{
private int length;
private int width;
private int height;
public void setLength(int p)
{length = p;}
public void setWidth(int p)
{width = p;}
public void setHeight(int p)
{height = p;}
public int displayVolume()
{System.out.println(length*width*height)...
}
public static void main(String args [ ])
{
Box b1=new Box(3,4,5);
b1.displayvolume();
}
}
Abstraction Example:
public class Animal
{
double energyReserves;
boolean isHungry() {
return energyReserves < 2.5;
}
void eat(Food f) {
energyReserves += f.getCalories();
}
public static void main(String[] args)
{
thePig = new Animal();
theCow = new Animal();
if (thePig.isHungry()) {
thePig.eat(tableScraps);
}
if (theCow.isHungry()) {
theCow.eat(grass);
}
}
}
/*In the above example, the class Animal is an abstraction used in place of an actual animal.*/
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.