In this article, you will learn Abstraction in Java Programming Language. The word ‘Abstraction’ generally means something whose execution details are not visible, you can only see and comply with the results.
For Example, if you are searching something in your web browser, the server will return a web page associated with the search keywords you have entered. All the execution details including the conversion of search keywords into HTTP request which are further converted into web pages by the web server are all hidden to you.
Similarly in Java Programming Language, Abstraction is the process of hiding all the execution details by declaring the class, interface and methods with an ‘abstract’ keyword.
Let us now go through how to achieve abstraction by using the keyword ‘abstract’ in a class and method.
Also Read how to Modifiers Used In Java Programming Language
ABSTRACT CLASS
Syntax:
public abstract class Classname { }
Declaration Rules:
- An abstract class cannot be instantiated and its fields and methods cannot be accessed, doing this will throw an error.
- A subclass that inherits the abstract class has to be created in order to instantiate and access fields and methods.
- It is not necessary that an abstract class should contain abstract methods, it may contain non abstract methods as well.
- A class must be declared with ‘abstract’ keyword if it contains abstract methods.
Example 1: An example of Data Abstraction using an abstract class ‘W3trainingSchool’ without inheriting it by a subclass ‘W3TS’ is shown below.
abstract class W3TrainingSchool {
float c ;
public void division ( float a, float b ) {
c = a / b ;
System.out.println(“Division =” + c) ;
}
public void modulus ( float a, float b ) {
c = a % b ;
System.out.println(“Modulus =” + c) ;
}
}
class W3TS {
public static void main (String args[]) {
float a = 50, b = 20 ;
W3TrainingSchool obj = new W3TrainingSchool ( ) ;
obj.division( a, b ) ;
obj.modulus( a, b ) ;
}
}
Output 1: After compiling the above program, following output will be generated.
Main.java:25: error: W3TrainingSchool is abstract; cannot be instantiated
W3TrainingSchool obj = new W3TrainingSchool ( ) ;
^
1 error
Explanation :
Clearly, the program will generate an error as the instantiation has been done without inheriting(extending) the abstract class ‘W3TrainingSchool’ by the subclass ‘W3TS’.
Example 2: An example of Data Abstraction using an abstract class ‘W3trainingSchool’ which is inherited by a subclass ‘W3TS’ is shown below.
abstract class W3TrainingSchool {
float c ;
public void division ( float a, float b ) {
c = a / b ;
System.out.println(“Division =” + c) ;
}
public void modulus ( float a, float b ) {
c = a % b ;
System.out.println(“Modulus =” + c) ;
}
}
class W3TS extends W3TrainingSchool {
public static void main (String args[]) {
float a = 50, b = 20 ;
W3TS obj = new W3TS ( ) ;
obj.division( a, b ) ;
obj.modulus( a, b ) ;
}
}
Output 2: After compiling the above program, following output will be generated.
Division = 2.5
Modulus = 10.0
Explanation:
In this example, the instantiation has been done by inheriting the abstract class ‘W3trainingSchool’ by its subclass ‘W3TS’. Hence, it will not throw an error and successfully execute the codes.
ABSTRACT METHOD
Abstract methods are different from non abstract methods. They do not have a method body and end up with a semicolon.
Syntax:
public abstract datatype methodName ( ) ;
Example: An example of Data Abstraction using abstract methods is shown below.
abstract class W3TrainingSchool {
public abstract int sum(int a, int b) ;
public abstract int difference(int a, int b) ;
}
class W3TS extends W3TrainingSchool {
public int sum(int x, int y) {
return x+y ;
}
public int difference(int x, int y) {
return x-y ;
}
public static void main(String args[]) {
W3TS obj = new W3TS( ) ;
System.out.println(obj.sum(10, 5)) ;
System.out.println(obj.difference(20, 10)) ;
}
}
Output: The following output will be generated after running the above codes.
15
10
Also Check:- Polymorphism In Java Programming Language
We have provided you the best possible description on Data Abstraction in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.