0% found this document useful (0 votes)
6 views

Lec18_Abstraction

Uploaded by

m4vinayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lec18_Abstraction

Uploaded by

m4vinayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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).

 Abstract class have constructor


 It contain abstract methods or concrete methods or empty class or combination of both
methods.
 To use abstract method of class, we should extends the abstract class and use that
methods.
 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 create the object of abstract class.

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;

public abstract class Test {

abstract void example(); // abstract method

abstract void demo();


}

How to implement those methods?

We need to create the class which extends from abstract class as shown in below.

package com.abstraction;

public class C extends Test {

@Override
void example() {

System.out.println("this is the example


method");

@Override
void demo() {

System.out.println("this is the demo method");

}
}

package com.abstraction;

public class TestMain {

public static void main(String[] args) {

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-

public interface Demo {

public abstract void m1();


public static final int a=5;
}

Example-

package com.abstra.interf;

interface A {

public abstract void demo();

public abstract void example();

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 {

public abstract void demo();

public abstract void example();

package com.abstra.interf;

public class Z implements A {

@Override
public void demo() {

System.out.println("this is demo method");

@Override
public void example() {

System.out.println("this is example method");

package com.abstra.interf;

public class TestMain {

public static void main(String[] args) {

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.

interface can extend interface1 and interface2


Interface can extends interface
Interface can extends the multiple interface
class extends class implements interface
class implements interface
class extends class implements interface1 and interface2

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.

Let's say SBI develops code like below:

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 {

void withdrawAmt(int amtToWithdraw) ;

class TransactionImpl implements Transactioni {

void withdrawAmt(int amtToWithdraw) {

//logic of withdraw

//SBI DB connection and updating in their DB

Now how amazon will do this as below as-

Transactioni ti = new TransactionImpl();

ti.withdrawAmt(500);

In this case, both application can achieve their aims.

You might also like