Skip to main content

OOPS Concept in Java : ABSTRACTION

 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;
}
}
Output will be 30 .

In above example if you notice we have override the sum() method with body in which we are adding 2 number. But while creating Object we have used INTERFACE Calculator reference named as cal and we are calling sum() method with the reference of INTERFACE not CLASS. So any other class wants to call this sum() method then we only need to give INTERFACE name not the CLASS name and through INTERFACE reference they can call sum() method.

This is all about INTERFACE.  Let's discuss about ABSTRACT CLASS now.

As mentioned earlier ABSTRACT CLASS  is for partial abstraction and ABSTRACT CLASS is similar to normal class the only difference is that we can't initiate ABSTRACT CLASS means can't create Object of ABSTRACT CLASS, we can have constructor, properties and behaviour just like other normal class.

Let's create an abstract class and to create an ABSTRACT class keyword is abstract.


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);
}
We can see inside abstract class we have created a sum() method with implementation while subtract has only declaration with keyword abstract.

Now we will create an Implementation class and write the body of subtract() method inside implementation class as below.


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;
}
}

Output will be 30 and 10.

Now we can clearly differentiate between INTERFACE and ABSTRACT class. INTERFACE can only have method declarations without body while ABSTRACT class can have both method declaration and method declaration with body. That's the reason I mentioned for partial abstraction we should use ABSTRACT class.

But after JAVA 8 release , we can have default and static method inside INTERFACE and this is an exceptional case. Why it was required and how can we create default and static method interface?

Consider we have develop whole application and everything is in place like all INTERFACES and CLASSES now a new behaviour(any new method) is required to add in already developed application. If INTERFACE has been implemented by 10 classes then all this 10 class needs to implement new method which we are going to declare in INTERFACE. To avoid such scenarios JAVA has introduced default method and it can be created as below by using default keyword.

interface Calculator{

default void defaultMethodExample(){
System.
out.println("I am from default method of interface");
}
}
Same way we can create static method in interface by using static keyword.

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");
}
}

And to run these method we can have Implementation class as below

class Sum implements Calculator {

public static void main(String args[]) {
Sum cal =
new Sum();
Calculator.staticExample();
cal.defaultMethodExample();
}
}

Output will be :  
I am from static method of interface
I am from default method of interface

Now you can have question what is difference between default and static method of INTERFACE, default method can be overridden by classes while static method can’t be .

 Please let me know in comment section if you have any doubt or confusion. Also i insist you to go through previous OOPS CONCEPT session from below link .



Comments

Popular posts from this blog

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...