Example of Abstraction in Java

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:

// 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