In this tutorial, we will write a java program to demonstrate abstraction. Before that, you should have knowledge on the following topic in Java.
Example of Abstraction in Java
Source code: Example of abstract class in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // Abstract class abstract class AbstractTest { public abstract void animals(); //abstract method public void dog() { System.out.println("Dog barks"); } } class Cat extends AbstractTest //inheriting AbstractTest class { public void animals() { // animals body System.out.println("Cat meows"); } } public class Main { public static void main(String[] args) { Cat c = new Cat(); // Create an object c.animals(); c.dog(); } } |
Output:
Cat meows
Dog barks