Java Abstract Classes and Methods

JK 2018 
Created at
Updated at  
7,708 0 0

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 (which you will learn more about in the next chapter).

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.

// 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();
  }
}

 

Below YouTube content might be helpful for your better understanding:

 

 

Tags Java Abstract Classes and Methods Java Abstraction Facebook X
Comments 0
  1. Java Servlet Example
    7,113
  2. How do I replace content that based on the HTML UI Template
    7,149
  3. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,162
  4. Dataset of California Foodbanks
    7,167
  5. Java Tutorials associated with AP Computer Science A
    7,593
  6. Java Inner Classes
    7,038
  7. Java Polymorphism
    7,049
  8. Java Inheritance (Subclass and Superclass)
    7,174
  9. Java Packages
    7,167
  10. Java Classes and Objects
    7,072
  11. Java Recursion
    7,192
  12. Java Scope
    7,082
  13. Java Methods
    7,072
  14. Java Arrays
    8,674
  15. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,840
  16. Java Switch Statements
    7,396
  17. Java Short Hand If...Else (Ternary Operator)
    7,567
  18. Java If ... Else
    7,388
  19. Java Math
    7,427
  20. Java Variables
    7,456
  21. Java Comments
    7,989
  22. The Print() Method
    7,144
  23. Java Syntax
    7,432
  24. Java Getting Started
    7,398
Recently updated
  1. Watch This Dazzling Dancer’s Mind-Blowing Moves
    24
  2. Michael Jackson's Billie Jean
    104
  3. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    126
  4. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    363
  5. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    217
  6. The Evolution and Production Reality of Agentic AI
    207
  7. How to Activate or Waive Your UIUC Student Health Insurance
    290
  8. Complete Guide to Building a Machine Learning Model
    322
  9. My life cuts at Las Vegas during Thanksgiving day holiday
    7,401
  10. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    347
  11. Harness vs. OpenClaw: Two Very Different "Agents"
    969
  12. Clean Python Environments: The Power of venv vs. Docker
    819
  13. What is Docker? Why is Docker also useful in a development environment?
    614
  14. UIUC 2026-2027 Academic Calendar
    1,572
  15. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    806
  16. Open-Source LLMs: The AI Revolution
    755
  17. Resume 2.0: Leveling Up for My First Software Gig
    2,210
  18. Not everyone will understand what this man just did
    1,858
  19. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,587
  20. Unpacking IU's Shopper
    755
  21. Jackie Chan's Police Story: The Action Masterpiece
    653
  22. The IVE Story: Identity, 'I AM' Charts, and Influence
    954
  23. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,187
  24. Open Databases for Sex Crime Occurrences in the U.S.
    713
  25. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,635
  26. My First Day at University of Illinois-Urvana Champaign
    1,188
  27. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,128
  28. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,208
  29. Sipping the Stars: My Starbucks Adventure
    9,650
  30. Exciting explore at Sequoia National Park
    7,654
  31. My Life Shot at Death Valley
    1,703
  32. Ip Man fights with Muay Thai Master
    939
  33. Mad Clown - Don't Die
    1,017
  34. How to get Student Enrollment and Degree Verification at UIUC
    4,868
  35. LAX Thanksgiving Rush: A Joyful Reunion
    921
  36. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,126
  37. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    971
  38. 10CM - To Reach You (너에게 닿기를)
    1,154
  39. Feeling weak? Transform yourself at the UIUC ARC!
    1,572
  40. BOYNEXTDOOR - If I Say I Love You
    1,183
  41. The Future of Software Engineer - AI Engineering
    927
  42. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    901
  43. Lie - Legend song by BIGBANG
    7,813
  44. Why ROLLBACK is useful when you work with Google Gemini CLI?
    833
  45. Reimbursement after Vaccination at McKinley Health Center
    1,017
  46. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    966
  47. Common Questions from UIUC school life in terms of CS Program
    1,119
  48. UIUC Immunization Compliance
    1,212
  49. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,091
  50. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,092