JAVA OOPS CONCEPT ABSTRACTION :
In Java , we need to hide our business logic and actual implementation from others and to achieve this we have 2 options. We can use INTERFACE or ABSTRACT CLASS . Let me tell you the difference between these 2 type of abstraction. INTERFACE should be used when you want 100% abstraction while for partial abstraction we can create ABSTRACT class.
Let's understand through an example :
if you need to buy a mobile, so you will go to mobile shop and you will buy a mobile, you won't go to manufacturer company of mobile and ask how they created mobile. right. Same way in Java we will create an implementation class and abstraction to use it without showing actual implementation . For example consider a Calculator class which can do all arithmetic operation like addition , subtraction , multiplication and divide. So if any one wants to use our class we should not allow to use our class directly, if this is allowed then they can make any kind of changes as per their need.
To avoid such things we need to put Abstraction. Now what is interface ?
Interface is the list of Properties and behaviour without implementation only declaration will be present inside INTERFACE , I already explained in detail about properties and behaviour in my previous post JAVA BASIC OVERVIEW. Its kind of contract between INTERFACE and CLASS and this what CLASS is going to implement and the keyword to implement any INTERFACE by any CLASS is implements.
Please don't get confuse between keywords extends and implements , implements will be used when a CLASS wants to implements a INTERFACE and extends will be used when a CLASS wants to extend properties and behaviours of parent class. I already explained extends keyword in details in my previous post INHERITANCE IN JAVA.
INTERFACE can have only static variables and abstract method. Abstract method means method declaration only without implementation of actual body as below (NOTE : I will consider the same example as my previous post so that it can help you to understand in simplest way):
interface Calculator{
public static int a=10;
public static int b=20;
public int sum(int fNumber, int sNumber);
}
In above INTERFACE we have declared 2 static variable and one method declaration. Static means it doesn't belongs to Object as explained in my previous post JAVA BASIC OVERVIEW and method has no body only method deceleration is present in INTERFACE, only method declaration without body can be declare only in INTERFACE not in CLASS . A class can not have only method declaration without body as its a Behaviour of class while in case of INTERFACE it's a behaviour declaration which must be implemented by all class which are going to implement the interface.
Let's create an implementation class which is going to implement above INTERFACE Calculator.
class Sum implements Calculator{
public static void main(String args []) {
Calculator cal = new Sum();
System.out.println(cal.sum(a, b));
}
@Override
public int sum(int fNumber, int sNumber) {
return fNumber+sNumber;
}
}
abstract class AbstractCalculator{
public static int a=10;
public static int b=20;
public int sum(int fNumber, int sNumber){
return fNumber+sNumber;
}
public abstract int subtract(int fNumber, int sNumber);
}
class Sum extends AbstractCalculator{
public static void main(String args []) {
Sum cal = new Sum();
System.out.println(cal.sum(a, b));
System.out.println(cal.subtract(a, b));
}
@Override
public int sum(int fNumber, int sNumber) {
return fNumber+sNumber;
}
@Override
public int subtract(int fNumber, int sNumber) {
return sNumber-fNumber;
}
}
interface Calculator{
default void defaultMethodExample(){
System.out.println("I am from default method of interface");
}
}
interface Calculator{
default void defaultMethodExample(){
System.out.println("I am from default method of interface");
}
static void staticExample(){
System.out.println("I am from static method of interface");
}
}
class Sum implements Calculator {
public static void main(String args[]) {
Sum cal = new Sum();
Calculator.staticExample();
cal.defaultMethodExample();
}
}
Comments
Post a Comment