Abstraction
Abstraction
Abstraction
It is the process of hiding the some details and showing the important
information to the end user called as “Abstraction”.
Example-
1. Car
2. ATM Machine
3. TV Remote
1. Abstract class
2. Interface
Abstract class
To use abstract method of class, we should extends the abstract class and
abstract.
abstract
Note- Multiple inheritances are not allowed in abstract class but allowed in
interfaces
Example-1
package com.abstraction;
}
Here, method is the abstract then class should be abstract only as per below
example
package com.abstraction;
Example-2- we can write multiple abstract method into abstract class as per below
package com.abstraction;
We need to create the class which extends from abstract class as shown in
below.
package com.abstraction;
@Override
void x1() {
System.out.println("x1 method..");
}
@Override
void x2() {
System.out.println("x2 method..");
}
}
package com.abstraction;
public class TestMain {
}
}
Note- Suppose in the sub class, I don’t want to override the abstract methods then
make that subclass as abstract.
Interface-
5. In interface, we can just define the method only but implemented those
methods into implemented class.
7. In 1.8 Declare the default & static method with body in interface.
9. Java supports multiple inheritance in the terms of interfaces but not classes.
interface interface_name {
Example-1
public interface A {
public abstract void x1(); // allowed
Note- if we don’t write public or abstract in interface then JVM will insert it
automatically.
Example-3
package com.abstraction;
public interface A {
public abstract void x1(); // allowed
}
package com.abstraction;
@Override
public void x1() {
System.out.println("Test-x1 method");
}
package com.abstraction;
Output
Test-x1 method
Example-4
package com.abstraction;
public interface A {
package com.abstraction;
public interface B {
package com.abstraction;
Example-5
package com.abstraction;
public interface A {
public abstract void x1(); // allowed
}
package com.abstraction;
public interface B {
public abstract void x1(); // allowed
}
package com.abstraction;
@Override
public void x1() {
System.out.println("Test-x1 method");
}
package com.abstraction;
package com.abstraction;
Below are the list of possible scenario regarding the interface and
Why interface?
Suppose there is a requirement for Amazon to integrate SBI bank code into their
shopping cart. Their customers want to make payment for products they
purchased.
class Transaction {
void withdrawAmt(int amtToWithdraw) {
//logic of withdraw
// SBI DB connection and updating in their DB
}
}
Amazon needs this class so they request SBI bank for the same. The problem with
SBI is that if they give this complete code to amazon they risk exposing everything
of their own database to them as well as their logic, which cause a security
violation.
Now the solution is for SBI to develop an Interface of Transaction class as shown
below:
interface Transactioni {
//logic of withdraw
ti.withdrawAmt(500);