0% found this document useful (0 votes)
14 views3 pages

Abstraction

Abstract classes and methods allow for abstraction by hiding implementation details and exposing only essential information. Abstract classes cannot be instantiated and require subclasses to provide implementations for abstract methods. An abstract class can contain both abstract and regular methods, where abstract methods have no body but subclasses must implement them. Abstract classes are used to achieve security and data abstraction by hiding details and only showing important object information to users.

Uploaded by

Brian Kanyua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Abstraction

Abstract classes and methods allow for abstraction by hiding implementation details and exposing only essential information. Abstract classes cannot be instantiated and require subclasses to provide implementations for abstract methods. An abstract class can contain both abstract and regular methods, where abstract methods have no body but subclasses must implement them. Abstract classes are used to achieve security and data abstraction by hiding details and only showing important object information to users.

Uploaded by

Brian Kanyua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Abstraction

Abstract Classes and Methods

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 we used in the Polymorphism chapter to an abstract class:
Remember from the Inheritance chapter 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();

Why And When To Use Abstract Classes and Methods?

To achieve security - hide certain details and only show the important details of an object.

Note: Abstraction can also be achieved with Interfaces,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy