In this article we learn the what is abstraction in java, real time example of abstraction, abstract class and method in java and advantages of abstraction in java.
What is Abstraction in Java
The meaning of the word “Abstraction”, in general words, is the process of working with ideas rather than their implementation.
Abstraction in Java OOPs Concept is a process of hiding the implementation details and showing only functionality to the user.
Abstraction in Java Example
1. ATM Machine
We all use an ATM machine for cash withdrawal, money transfer, retrieve min-statement, etc. in our daily life.
But we don’t know internally what things are happening inside ATM machine when we insert an ATM card for performing any kind of operation.
2. Car
A car owner knows that the accelerator pedal is used to increase the speed of car and pressing the brake pedal for stops it.
To perform these simple actions, we only need to know how to use these components but not need to know how they function.
3. SMS Send
When we need to send SMS from our mobile, we only type the text and send the message. But we don’t know the internal processing of the message delivery.
We only need to call the specific classes or methods to implement specific program logic, but we don’t know how these classes or methods function. This concept is known as abstraction in java.
Abstract Class in Java
An Abstract class is created through the use of the abstract keyword. It is used to represent a concept.
An abstract class can have abstract methods (methods without body) as well as non-abstract methods or concrete methods (methods with the body). A non-abstract class cannot have abstract methods.
The class has to be declared as abstract if it contains at least one abstract method.
There is always a default constructor in an abstract class, it can also have a parameterized constructor.
The abstract class can also contain final and static methods.
Syntax:
abstract class ClassName {
//class body
}
Abstract Method in Java
Abstract methods are methods with no implementation and without a method body. They do not contain any method statement.
An abstract method is declared with an abstract keyword.
The declaration of an abstract method must end with a semicolon (;).
The child classes which inherit the abstract class must provide the implementation of these inherited abstract methods.
Any method which has both declaration and implementation is called as non- abstract method / Concrete Method.
When we create abstract method then that class becomes incomplete.
If a class is declared as abstract then it may /may not contain abstract method.
We can develop only concrete method inside the abstract class. It is not mandatory that an abstract class should have abstract method.
A method can be declared as final such methods can’t be override in the sub class.
Can a class in Java be both final and abstract?
No, if we declare a class abstract, to use it, we must extend it and if we declare a class final we cannot extend it, since both contradict with each other we cannot declare a class both abstract and final if you do so a compile time error will be generated.
Example:
public final abstract class Example {
public void sample () {
}
}
Compile time error
Example: java:1: error: illegal combination of modifiers: abstract and final public final abstract class Example {
1error
Can we define a parameterized constructor in an abstract class in Java?
Yes, we can define a parameterized constructor in an abstract class.
Conditions for defining a parameterized constructor in an abstract class:
We need to make sure that the class which is extending an abstract class have a constructor and it can call the superclass parameterized constructor.
We can call the superclass parameterized constructor in a subclass by using super () call.
If we are not placing super () call in the subclass constructor, a compile-time error will occur.
Example :
abstract class AbstractClassTest {
AbstractClassTest (int a) {
// Parameterized Constructor
System.out.println(“Parameterized Constructor of an abstract class a=”+ x);
}
}
public class Test extends Abstract Demo {
Test () {
super (20).
System.out.println(“Test Class Constructor”);
}
public static void main (String [] args) { Test obj = new Test ();
}
}
Output:
Parameterized Constructor of an abstract class a=20 Test Class Constructor
In the above example, we must place a super () call in the subclass constructor (Test), if not a compile-time error will occur.
Can we create object of abstract class?
Yes, we can create the object of abstract class.
If we create object of abstract class according to regular syntax, then we will get error.
Syntax of object:
className ObjectName =New className ();
All abstract methods should be defined in the curly bracket.
Example:
abstract class parent {
abstract public void test (int a, int b);
abstract public void test1 ();
}
public class AbstractClassObjectCreation {
public static void main (String [] args) {
// parent obj1=new parent ();
// Error -Object creation parent obj2=new parent () {
public void test (int a, int b) {
System.out.println(“Sum=”+(a+b));
}
public void test1() {
System.out.println(“Test1”);
}
}
obj2.test(10,20);
obj2.test1();
}
}
Advantages of Abstraction in Java
Abstraction in java reduces the complexity of viewing things.
Increases software reuse and avoids code duplication: Loosely coupled classes often prove useful in other contexts.
Abstraction in java helps to increase the security and confidentiality of an application as only necessary details are exposed to the user.
Eases the burden of maintenance – Classes can be understood more quickly and debugged with little fear of harming other modules.
Other Popular Articles
What is Object Oriented Programming (OOPs) Concepts in Java?