• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

How to Achieve Abstraction in Java | Real Programming Example

Pranati Paidipati/13932/0
CodeJAVA

Before proceeding to the Java programming, you should know the meaning of abstraction.

Table of Contents

  • Abstraction Definition
  • What is an Abstract class in Java?
  • Why do you need Abstract Classes in Java?
  • Abstract Class Java Programming Example

What is Abstraction?

The details of what an object is doing but knowing how is being done are referred to as Abstraction.

Not clear?

In other words, the process of hiding the implementation details from the client and showing the basic functionality is an Abstraction.

It is one of the principle Object Oriented Concepts in Java.

Abstraction is accomplished in Java with the help of abstract classes and interfaces.

What is an Abstract Class in Java?

A class that is declared using the keyword abstract is referred to as an abstract class.

Such classes may have both abstract methods (methods without any implementation) and concrete methods(methods with implementation).

Note that, a class that is declared as abstract cannot be instantiated.

One basic rule to be followed with abstract classes is that if any abstract class is extended then that class must have an abstract method, or it should provide the implementation of the method. Otherwise, make the extended class also abstract.

Why is there a need of Abstract Classes in Java?

Let’s take a simple and real example:

Suppose we have a class named SmartPhone that consists of a method androidVersion() and the child classes of this parent class are Xiomi, Moto, Oppo, Nokia, etc.

As we know, every smartphone differs in its version of Android, thus making it useless to define the androidVersion() method in the parent class.  The reason behind this is every smartphone will have to override the method and employ its own details of version like Xiomi can have Android One, Moto can have Android Nougat, etc.

So when the implementation of the method androidVersion() is different for each inherited classes, making the method abstract is the best option.

Now, since the method is the abstract method, declaring the class as abstract is also needed.

Program: How to Achieve Abstraction in Java?

abstract class SmartPhone
{
    abstract  void androidVersio();
    void play()
    {
        System.out.println(“Play music and games”);
    }
}

class Nokia extends Smartphone
{
    void androidVersion()
    {
        System.out.println(“Android 8.1 : Oreo”);
    }
}

class Test
{
    public static void main(String args[])
    {
        SmartPhone s = new Nokia();
        s.androidVersion();
        s.play();
    }
}

Output:

Android 8.1: Oreo
Play music and games

To sum it up, the concept of abstraction deals with the hiding of implementation details and giving the user the access to basic functional details. You can achieve abstraction in Java using abstract classes and interfaces.


« Method Overloading vs OverridingInterfaces and Inheritance in Java »

JavaJava Oops Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- HashSet
  6. Java- HashMap vs HashSet
  7. Java- Reverse Linked List

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard