Abstraction

ARJUN N U
2y

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!
Add answer anonymously...
Tech Mahindra Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter