Abstraction in Java
Abstraction is a process of hiding details about implementation from the user while letting them utilise the services or functions. Therefore, the user will know what an object does, but not how it does it. It is done to simplify the user interface while hiding the complex processes.
- Exposing only required or important things is called abstraction
- Abstraction can be done in two ways :
- Abstract class
- Interface
- It is used for maintaining the standards of coding in project
Interface
One of the java principles says that we must follow the I to C design. This means that every class should be implemented from Interface
In the industry, architect level people create interfaces and then it is given to the developers for writing classes by implementing interfaces provided
This is best way to expose our project’s API to some other projects. For example, ICICI bank can expose methods or interfaces to various shopping carts
The interface is the same as ordinary class but remember below points
We cannot instantiate Interface but we can create reference variables
The interface doesn't have a constructor
It can be compiled but cannot run
Interface can extends interface
The interface can extend multiple interfaces
-
Example:
void m1(); //No body Method- In case of classes, we must define the body of method
package com.abstraction; public class X { void m1(); // compile time error method should have body } - In case of classes, if we want the above written lines to compile when we have method without body, then we must explicitly define that method as abstract in case of classes, but not in case of interface
package com.abstraction; public class X { abstract void m1(); // compile time error } - In case of classes, if method is abstract then class must be declared as abstract as well.
package com.abstraction; public abstract class X { abstract void m1(); // No error } - In case of interface we don't need to do explicit declaration for methods like class but they are, by default, abstract
package com.InterFace; interface Xi { void m1(); // abstract keyword invisibly present } - All methods are public in interface. If you want to mention access specifiers, mention them as public, or do not mention any access specifier and JVM will automatically consider it as public, every time
package com.InterFace;
interface xyz {
void m1(); // public and abstract invisibly put by JVM
}
package com.InterFace;
public interface x {
public abstract void m1(); //OK
public void m3(); //OK
abstract void m6();//OK
void m7();//OK
}
- Variables are public final static in interface and act like constants
- If we want to use methods of interface, we have to implement that interface in any class, and methods of interface must be implemented in a class by using the implements keyword
package com.InterFace;
interface Xi {
void m1();
}
package com.InterFace;
public class XImpl implements xi{
//Not compiled as we have not implemented all the methods
}
package com.InterFace;
public interface Ai {
void m1(); //OK
}
package com.InterFace;
public interface Bi {
void m1(); //OK
}
package com.InterFace;
public interface Ci extends Ai,Bi { //OK
void m1();
}
This is allowed in java
- interface can extend interface1 and interface2
- class extends class implements interface
- class implements interface
- class extends class implements interface1 and interface2
Q. Why do we need Interface?
A: To expose the third party APIQ. Why not constructor?
A: In interface it will not get called by using super by using hierarchyQ. As an interface has abstract method, how will you use the abstract method of interface?
A: Implement that method in class, or in other words override methods, and then create object of that class and call all methodsQ. Have you ever used others third party API code?
A:Yes, they have their Interface and API and we can use that.
Why interface?
Suppose there is a requirement for Flipkart to integrate ICICI bank code into their shopping cart.Their customers want to make payment for products they purchased. Now, to make payment, Flipkart does not have their own bank, so it must take help of other banks.
Let's say ICICI develops code like below:
class Transaction {
void withdrawAmt(int amtToWithdraw) {
void m1(); // abstract keyword invisibly present
//logic of withdraw
//ICICI DB connection and updating in their DB
}
}
Flipkart needs this class so they request ICICI bank for the same. The problem with ICICI is that if they give this complete code to flipkart they risk exposing everything of their own database to them as well as their logic, which is a security violation.
Now, what exactly Flipkart needs is the method name and class name. But to compile their class they must get one like below:
Transaction t=new Transaction(); //1
t.withdrawAmt(500); //2
If the first line to compile at Flipkart ends, then they must have this class which ICICI cannot give. The best solution here is for ICICI to develop an Interface of Transaction class as shown below:
interface Transactioni {
void withdrawAmt(int amtToWithdraw) ;
}
class TransactionImpl implements Transactioni {
void withdrawAmt(int amtToWithdraw) {
//logic of withdraw
//ICICI DB connection and updating in their DB
}
}
Flipkart will now get the interface but not class, and they can use it as shown below:
Transactioni ti = new TransactionImpl();// the result on the right hand side may be achieved by webservices or EJB TransactionImpl physically not present at Flipkart
ti.withdrawAmt(500);In this case, both parties achieved their goal, Flipkart which only needs method name and parameter and they got it
ICICI only wants to give them the name but not their logic, which they provided. Also it does not matter to ICICI what you purchased, they just want to have amount to be deducted
Abstract Class
Abstract class is just like a normal class but with some extra features which we will see in this chapter
Abstract class can have constructor
It can have abstract methods or concrete methods or nothing [empty class] or mix of both methods
To use abstract method of class, we must extend the abstract class with ordinary class or must implement all abstract methods in that ordinary class
If we don't want to implement or override that method, make those methods as abstract
If any method is abstract in a class then that class must be declared as abstract
We cannot instantiate abstract class
Multiple inheritances are not allowed in abstract class but allowed in interfaces
Abstract classes are useful if we want others to not call our class directly but though Inheritance
Abstract static final combinations are not allowed in java
Q. Why does the abstract class have a constructor even though you cannot create object?
A: Abstract class can only be extended by class and that class will have its own constructor and that constructor will have super (); which is calling constructor of abstract class. If the abstract class doesn't have constructor then the class extending that abstract class will not get compiled. Therefore, abstract classes have constructors.Q. Can the abstract class have concrete methods only [concrete method means method with body]?
A: Yes.Q. If abstract class can have both types of methods [one having body and other without body] then what is use of those methods?
A: We can use those methods by (1) extending abstract class or (2) making that method as a static so that we will not need to create an object.Q. How to call m2() which is concrete of abstract class?
A: Make it as static and call asKiran.m2 (); //where Kiran is class nameQ. Can abstract class extend ordinary class?
A: Yes.
Difference between Abstract Class & Interface
| Abstract Class | Interface |
|---|---|
|
|
|
|
|
|
|
|