Abstraction

ARJUN N U
3y

Data abstraction is the process of hiding certain details and showing only essential information to the user.

Abstraction can be achieved with either abstract classes or interfaces.

 

The abstract keyword is a non-access modifier, used for classes and methods:

 

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

 

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

 

abstract class Animal {

  public abstract void animalSound();

  public void sleep() {

    System.out.println("Zzz");

  }

}

 

 

From the example above, it is not possible to create an object of the Animal class:

 

Animal myObj = new Animal(); // will generate an error

To access the abstract class, it must be inherited from another class. Let's convert the Animal class to an abstract class:

 

Remember from the Inheritance concept that we use the extends keyword to inherit from a class.

 

Example

// Abstract class

abstract class Animal {

  // Abstract method (does not have a body)

  public abstract void animalSound();

  // Regular method

  public void sleep() {

    System.out.println("Zzz");

  }

}

 

// Subclass (inherit from Animal)

class Pig extends Animal {

  public void animalSound() {

    // The body of animalSound() is provided here

    System.out.println("The pig says: wee wee");

  }

}

 

class Main {

  public static void main(String[] args) {

    Pig myPig = new Pig(); // Create a Pig object

    myPig.animalSound();

    myPig.sleep();

  }

}

src: w3schools

I hope this helps. If you need any further help

Help your peers!
Select
Add answer anonymously...

Tech Mahindra Java Developer interview questions & answers

A Java Developer was asked 2mo agoQ. Describe stream APIs.
A Java Developer was asked 7mo agoQ. Are you willing to work in any location?
A Java Developer was asked 7mo agoQ. What are the differences between method references and lambda functions?

Popular interview questions of Java Developer

A Java Developer was asked 2mo agoQ1. Describe stream APIs.
A Java Developer was asked 7mo agoQ2. Are you willing to work in any location?
A Java Developer was asked 7mo agoQ3. What are the differences between method references and lambda functions?
Tech Mahindra Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits