0% found this document useful (0 votes)
32 views17 pages

BCA Abstraction

Abstraction in Java can be achieved through abstract classes and interfaces. Abstract classes can contain both abstract and concrete methods while interfaces contain only abstract methods. Abstract classes allow for 0-100% abstraction by extending the class and implementing abstract methods. Interfaces provide 100% abstraction and allow for multiple inheritance by implementing multiple interfaces.
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views17 pages

BCA Abstraction

Abstraction in Java can be achieved through abstract classes and interfaces. Abstract classes can contain both abstract and concrete methods while interfaces contain only abstract methods. Abstract classes allow for 0-100% abstraction by extending the class and implementing abstract methods. Interfaces provide 100% abstraction and allow for multiple inheritance by implementing multiple interfaces.
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Abstraction in Java

Abstraction in Java

• Abstraction is a process of hiding the implementation details and showing


only functionality to the user.

• For example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery.
Ways to achieve Abstraction
• There are two ways to achieve abstraction in java.
1. Abstract class (0 to 100%)

2. Interface (100%)
1. Abstract class in Java

• A class which is declared as keyword abstract is known as an abstract


class.

• It can have abstract and non-abstract methods.


Points to Remember

• An abstract class must be declared with an abstract keyword.

• It can have abstract and non-abstract methods.

• We can not create the object of abstract class.


Cont…
• It needs to be extended in another class and its abstract method should
be implemented in that class.
Example of abstract class
abstract class A
{

}
Abstract Method in Java

• A method which is declared as abstract and does not have body is


known as an abstract method.

• Example of abstract method


abstract void A();//no method body and abstract  
Example 
abstract class A
{
abstract void run();
void display()
{
System.out.println("Bike");
}
}
class B extends A
{
void run()
{
System.out.println("running safely");
}
public static void main(String args[])
{
B b1 = new B();
b1.run();
b1. display();
}
}
Cont…
Output:
running safely
Bike
2. Interface in Java

• It has only abstract methods, not method with body.

• It is used to achieve 100 % abstraction and multiple inheritance in


Java.
Why use Java interface?
• It is used to achieve 100% abstraction.

• By interface, we can support the functionality of multiple inheritance.


Note:
• Like abstract classes, object of interfaces can not be created.

• It needs to be implements in a class and its method should be


implemented in that class.

• Interface methods are by default abstract and public.


Cont…
• Interface attributes are by default public, static and final.

Example:
Example
interface A
{
void print();
}
class B implements A
{
public void print()
{
System.out.println("Hello");
}
public static void main(String a[])
{
B b1 = new B();
b1.print();
}
}
Cont…
Output:
Hello
Multiple inheritance with the help of
Interface
interface X
{
void display();
}
interface Y
{
void display();
}
class C implements X, Y
{
public void display()
{
System.out.println("Hello");
}
public static void main(String x[])
{
C c1 = new C();
c1.display();
}
}
Output:
Hello

You might also like