Open In App

Abstraction by Parameterization and Specification in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Abstraction by Parameterization and specification both are important methods in Java. We do this in hope of simplifying our analysis by separating attributes and details implementation for user requirements by showing the essential part to the user and hiding certain details for various purposes like security, maintenance, etc.

  • Abstractions help us in many ways like an easy way to arrange code.
  • It also helps in decomposing big steps into smaller steps.
  • It is useful for suppressing details and simplifying interaction.
  • Abstraction plays an important role in improving the maintenance part of the project.

Example:

Basically, abstraction is a method to provide simplicity by hiding all the mechanisms behind them. For example when someone calls you so you see on your screen that XYZ is calling you with a green icon to receive and a red icon with a reject option and some messaging option but, what you do not see is all the backend mechanisms that how someone is calling you how by tapping on-screen icon you can receive/reject or text someone how it’s going to connect to caller all the things. This way abstraction makes things simple.

Java




// A sample program to demonstrate
// Abstraction program in Java
 
// Consider a Student_Record class.
 
abstract class Student_Record {
   
    // It's an Abstract method does
    // not contain body.
    public abstract void Student_Fee_Record();
 
    // Student_Info is a normal method.
    public void Student_Info()
    {
        String name = "Ashish";
        int roll_no = 12345;
        System.out.println("Hello! " + name
                           + " your roll_no is :"
                           + roll_no);
    }
}
 
// Student_Fee is a subclass which
// is extended from Student_Record.
class Student_Fee extends Student_Record {
    public void Student_Fee_Record()
    {
        int Fee = 1000;
 
        // Here is provided body of
        // Student_Fee_Record().
        System.out.println("Fee :" + Fee);
    }
}
 
class Student_Data {
    public static void main(String[] args)
    {
        
      // Create a Student_Fee object
       Student_Fee s1
            = new Student_Fee();        
        s1.Student_Fee_Record();
        s1.Student_Info();
    }
}


Output

Fee :1000
Hello! Ashish your roll_no is :12345

Methods of Abstraction :

  • Abstraction by Parameterization.
  • Abstraction by Specification.

Abstraction by Parameterization :

It abstracts from the identity of the data by replacing them with parameters.

Example –

x % 2  =  0

It describes a computation that when we divide a number x to 2 then the remainder is equal to zero which we use for finding that given number x is even or not.

x : int(x % 2 == 0)(y)

It is identical in meaning to 

y % 2==0

In more familiar notation, we might denote the previous expression by the following expression given below.

Java




class Abstraction {
 
    // Abstraction by parameterization
    int Even(int x)
    {
        if (x % 2 == 0)
            return x;
    }
}


Abstraction by Specification:

It abstracts from the implementation details (how the module is implemented) to the behavior users can depend on (what the module does). It isolates modules from one another’s implementations.

It allows us to abstract from the computation described by the body of a procedure to the end that procedure was designed to accomplish. We do this by associating with each other procedure a specification of its intended effect and then considering the meaning of a procedure call to be based on this specification rather than on the procedure’s body.

Even-odd procedure:

Java




int Even(int x)
 
{
 
    // REQUIRES: num x is only integer
    // MODIFIES: system.out
    // EFFECTS : is num x % 2==0
    // then num x is even else it is odd.
    if (x % 2 == 0) {
        system.out.print(num + "is even ") else system.out
            .print(num + "is odd")
    }
}


Example:

In this program, you will see how you can write the logic for a user like if the user wants only input any number and just to know whether a given number is odd or even the logic of the program is given below to check the even and odd for any user input number.

Input : 11
Output : Odd

Input : 52
Output : Even

Java




import java.util.Scanner;
 
public class CheckEvenOdd {
 
    // REQUIRES:num x is only integer
    // MODIFIES:System.out
    // EFFECT:is num x % 2 == 0
    // then x is even else odd
 
    public static void main(String args[])
    {
        int num;
        System.out.println("Enter an Integer number:");
 
        Scanner input = new Scanner(System.in);
        num = input.nextInt();
 
        if (num % 2 == 0)
            System.out.println("Entered number is even");
        else
            System.out.println("Entered number is odd");
    }
}


Input:

78

Output :

Enter an Integer number:
78
Entered number is even


Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads