Simple Example of Abstraction in Java
What is abstraction in java?
In java, abstraction is approach that is used for representing the key characteristics of a class without providing the implementation in classes and interfaces. We can say that abstraction is a technique that is used to show essential things to the user and hide the detail.
For example:
When you drive a car you can only see the essential detail like brakes, staring wheel, accelerator and all the detail like background work of these things are hidden.
Java abstraction feature enables the creation of reusable code that may be modified for many scenarios. It enables programmers to declare a collection of properties and methods that a class must have without defining the specifics of how they should operate.
This allows for flexibility and modularity in code design since multiple classes can implement the same abstract class or interface in various ways.
Syntax of abstraction in java:
In java, if we want to implement abstraction then we need to use abstract classes and interfaces:
Here are the syntax for each:
//abstract classes abstract class ClassName { // abstract method(s) // non-abstract method(s) } //interface syntax interface InterfaceName { // constant(s) // abstract method(s) }
Example of abstraction in java
Example 1:
//program to find the area of rectangle and triangle using abstraction in java import java.util.Scanner; public class _5Assignment { public static void main(String args[]) { System.out.println("This Program will calculate the area of Triangle and Rectangle"); System.out.println("Enter the Height: "); Scanner sc = new Scanner(System.in); float h = sc.nextFloat(); System.out.println("Enter the Base or Width: "); float b = sc.nextFloat(); Area a = new Triangle(); a.cal(h,b); Area c = new Rectangle(); c.cal(h,b); a.display(); c.display(); } } abstract class Area { abstract void cal(float a1 , float b1); void display() { System.out.println("The Calculated Area for the Triangle and Rectangle: "); } } class Triangle extends Area { double a; void cal(float h, float b) { this.a = ((0.5)*h*b); } void display() { super.display(); System.out.println("The area of triangle is: "+a); } } class Rectangle extends Area { double a; void cal(float h,float b) { this.a = (h*b); } void display() { System.out.println("The area of Rectangle is: "+a); } }
Output:
This Program will calculate the area of Triangle and Rectangle Enter the Height: 22 Enter the Base or Width: 12 The Calculated Area for the Triangle and Rectangle: The area of triangle is: 132.0 The area of Rectangle is: 264.0
Example 2:
//program to show the sound of animal using abstraction in java //Abstract class abstract class Animal { // Abstract method public abstract void makeSound(); } //Subclass of the abstract class class Cow extends Animal { public void makeSound() { System.out.println("Mooo!"); } } //Another subclass of the abstract class class Horse extends Animal { public void makeSound() { System.out.println("Neigh!"); } } class Pig extends Animal { public void makeSound() { System.out.println("oink oink!"); } } class Dog extends Animal { public void makeSound() { System.out.println("Woof!"); } } class Cat extends Animal { public void makeSound() { System.out.println("Meow!"); } } //Main class to demonstrate the usage of the abstract class public class Main { public static void main(String[] args) { Animal cow = new Cow(); cow.makeSound(); // Output: Mooo! Animal horse = new Horse(); horse.makeSound(); // Output: Neigh! Animal pig = new Pig(); pig.makeSound(); //output: oink oink! Animal dog = new Dog(); dog.makeSound(); // Output: Woof! Animal cat = new Cat(); cat.makeSound(); // Output: Meow! } }
Output:
Mooo! Neigh! Oink oink! Woof! Meow!