What is abstraction?
Abstraction is one of the important concept in OOPs. Abstraction, basically means hiding unnecessary details and showing relevant information to users. Take for example, when we learn to drive a car we do not care about how it engines work, we only cares about how it's steering, brake and accelerator will work. We never get into the complication of car's internal working.
In OOPs abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it.
There are basically two types of abstraction :
Data Abstraction : Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with data type, whereas hiding all the implementation details from outside work.
Advantage of this abstraction is that even if our internal implementation got change there will be no impact on client facing code since they only work with the exposed behavior not the internal implementation.
Control Abstraction : When we works on a application software, we sometimes write repetitive code at multiple places. Control abstraction is the process of identifying such statements and expose them as single unit of work. We normally use this feature when we create a function to perform any work.
How we achieve abstraction is Java?
In java abstraction is achieved by interfaces and abstract classes. Interfaces allows you to abstract implementation completely whereas abstract classes allows for partial abstraction.
Let's see an example for both of these:
Abstraction using interfaces
interface Animal{
public void voice();
public void eat();
}
class Dog implements Animal{
@Override
public void voice(){
System.out.println("Dog Barks");
}
@Override
public void eat(){
System.out.println("Dog eats Meat");
}
}
class Cat implements Animal{
@Override
public void voice(){
System.out.println("Cat Meows");
}
@Override
public void eat(){
System.out.println("Cat drink Milk");
}
}
class MainClass{
public static void main(String[] args){
Animal dog = new Dog();
dog.voice();
dog.eat();
Animal cat = new Cat();
cat.voice();
cat.eat();
}
}
Remember we can not instantiate objects of interfaces directly. When we execute above main class we will get below output on console.
Dog Barks
Dog eats Meat
Cat Meows
Cat drink Milk
For abstract class example let's repeat our Employee example from previous post.
abstract class Employee{
private String empName;
private String empId;
public void storeDetail(){
System.out.println("Storing employee details");
}
public void showDetail(){
System.out.println("Showing employee details");
}
public abstract void calculateSalary();
}
class PartTimeEmployee extends Employee{
@Override
public void calculateSalary(){
System.out.println("Calculating salary for part time employee.");
}
}
You might like to see : Difference between abstraction and interface.
This is all about abstraction is java, we will discuss about Encapsulation in next post.
Great Content.. I like it.
ReplyDeleteThanks.. :)
Delete