Lec18_Abstraction
Lec18_Abstraction
It is the process of hiding the certain details and showing the important information to the
end user called as “Abstraction”.
it shows only essential things to the user and hides the internal details, for example, sending SMS
where you type the text and send the message. You don't know the internal processing about the
message delivery.
How to achieve the Abstraction in java?
There are two ways to achieve the abstraction in java.
1. Abstract class
2. Interface
Abstract class
A class which is declared with the abstract keyword is known as an abstract class in Java. It can
have abstract and non-abstract methods (method with the body).
Note- Multiple inheritances are not allowed in abstract class but allowed in interfaces
Example-
public class Test {
abstract void demo ();
}
Here, method is the abstract then class should be abstract only.
public abstract class Test {
abstract void test();
void demo(){
//concrete methods here
}
package com.abstraction;
We need to create the class which extends from abstract class as shown in below.
package com.abstraction;
@Override
void example() {
@Override
void demo() {
}
}
package com.abstraction;
C c= new C();
c.demo();
c.example();
}
}
Note- Suppose in the sub class, I don’t want to override the abstract methods then make that
subclass as abstract.
Interface-
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
1. It contain public abstract methods and public static final variables by default.
2. We must follow I to C design principle in java. It means every class must be
implemented by some interfaces.
3. In company, Team Lead or Manager level people can design the interface then give it to
developer for implementing it.
5. Before 1.7, interface does not have any method body.
6. 1.8 Declare the default & static method with body in interface.
7. 1.9 we can define the private methods in interface also.
8. We cannot create the object of interface.
9. In interface, we can just define the method only but implemented that methods into
implemented class.
10. Java supports multiple inheritance in the terms of interfaces but not classes.
11. Interface does not have constructor.
Syntax
interface interface_name {
}
Example-
Example-
package com.abstra.interf;
interface A {
interface A{
public abstract void demo (); //allowed
public void demo (); //allowed
void demo (); //allowed
abstract void demo (); //allowed
}
Note- if we don’t write public or abstract in interface then JVM will insert it automatically.
package com.abstra.interf;
interface A {
package com.abstra.interf;
@Override
public void demo() {
@Override
public void example() {
package com.abstra.interf;
Z z = new Z();
z.demo();
z.example();
//A a= new A();
}
}
Example-2
interface A {
}
interface B {
}
Interface C extends A, B {
}
This is allowed in java.
Below are the list of possible scenario regarding the interface and
Note- Try this from your end on laptop or desktop.
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);