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

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA :  Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE  , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java.  But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT