0% found this document useful (0 votes)
6 views5 pages

Abstraction

Abstraction is a process in programming that hides implementation details and shows only essential functionality to the user. In Java, abstraction can be achieved through abstract classes and interfaces, where abstract classes can have both abstract and non-abstract methods but cannot be instantiated directly. Subclasses must implement any abstract methods defined in the abstract class to provide specific functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Abstraction

Abstraction is a process in programming that hides implementation details and shows only essential functionality to the user. In Java, abstraction can be achieved through abstract classes and interfaces, where abstract classes can have both abstract and non-abstract methods but cannot be instantiated directly. Subclasses must implement any abstract methods defined in the abstract class to provide specific functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Abstraction

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


functionality to the user.

Another way, 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.

Abstraction lets you focus on what the object

does instead of how it does it.

Ways to achieve Abstraction :

There are two ways to achieve abstraction in java

1. Abstract class (0 to 100%)


2. Interface (100%)

Abstract class :

▪ A class which is declared as abstract is known as an abstract class.

abstract class useAsbtract {


______________
______________
}

▪ It can have abstract and non-abstract methods.

abstract class useAsbtract {

void disp() {

System.out.println("In abstract class:");

abstract void show();

▪ It cannot be instantiated (we cannot create objects of abstract classes).


▪ It needs to be extended and its method implemented.

Java Abstract Method :

A method that doesn't have its body is known as an abstract method. We use the
same abstract keyword to create abstract methods. For example,
abstract void show();

Example: Java Abstract Class and Method :

Though abstract classes cannot be instantiated, we can create subclasses from it. We can
then access members of the abstract class using the object of the subclass. For example,

abstract class useAsbtract {


void disp() {
System.out.println("In abstract class:");
}
//abstract void show();
}
class subA extends useAsbtract {
//void show() {
// }
}
public class abstraction {
public static void main(String[] args) {
useAsbtract a = new subA();
a.disp();
}
}
Output:

Implementing Abstract Methods :

If the abstract class includes any abstract method, then all the child classes inherited from
the abstract superclass must provide the implementation of the abstract method. Otherwise get
an error. For example,

abstract class useAsbtract {


void disp() {
System.out.println("In abstract class:");
}
abstract void show();
}

Accesses Constructor of Abstract Classes :

abstract class useAsbtract {


useAsbtract() {
System.out.println("I am useAbstract class constructor:");
}
}
class subA extends useAsbtract {
subA() {
super();
}
}
public class abstraction {
public static void main(String[] args) {
useAsbtract a = new subA();
}
}
output :

Example:

abstract class useAsbtract {


useAsbtract() {
System.out.println("I am useAbstract class constructor:");
}
void disp() {
System.out.println("In abstract class:");
}
abstract void show();
}
class subA extends useAsbtract {
subA() {
super();
}

void show() {
}
}
public class abstraction {
public static void main(String[] args) {
useAsbtract a = new subA();
a.disp();
}
}

Output:

Interface:

You might also like