Java Abstraction.zip

Java Abstract Classes and Methods

Java Abstract Classes and Methods

7,704

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 i...





Java Abstract Classes and Methods

JK 2014 
Created at
Updated at  
7,705 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
Recently updated
  1. Michael Jackson's Billie Jean
    84
  2. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    123
  3. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    348
  4. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    214
  5. The Evolution and Production Reality of Agentic AI
    204
  6. How to Activate or Waive Your UIUC Student Health Insurance
    287
  7. Complete Guide to Building a Machine Learning Model
    321
  8. My life cuts at Las Vegas during Thanksgiving day holiday
    7,399
  9. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    346
  10. Harness vs. OpenClaw: Two Very Different "Agents"
    968
  11. Clean Python Environments: The Power of venv vs. Docker
    818
  12. What is Docker? Why is Docker also useful in a development environment?
    613
  13. UIUC 2026-2027 Academic Calendar
    1,568
  14. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    803
  15. Open-Source LLMs: The AI Revolution
    751
  16. Resume 2.0: Leveling Up for My First Software Gig
    2,206
  17. Not everyone will understand what this man just did
    1,843
  18. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,585
  19. Unpacking IU's Shopper
    754
  20. Jackie Chan's Police Story: The Action Masterpiece
    652
  21. The IVE Story: Identity, 'I AM' Charts, and Influence
    953
  22. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,187
  23. Open Databases for Sex Crime Occurrences in the U.S.
    712
  24. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,626
  25. My First Day at University of Illinois-Urvana Champaign
    1,185
  26. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,125
  27. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,203
  28. Sipping the Stars: My Starbucks Adventure
    9,646
  29. Exciting explore at Sequoia National Park
    7,652
  30. My Life Shot at Death Valley
    1,697
  31. Ip Man fights with Muay Thai Master
    933
  32. Mad Clown - Don't Die
    1,013
  33. How to get Student Enrollment and Degree Verification at UIUC
    4,857
  34. LAX Thanksgiving Rush: A Joyful Reunion
    920
  35. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,122
  36. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    971
  37. 10CM - To Reach You (너에게 닿기를)
    1,152
  38. Feeling weak? Transform yourself at the UIUC ARC!
    1,571
  39. BOYNEXTDOOR - If I Say I Love You
    1,181
  40. The Future of Software Engineer - AI Engineering
    927
  41. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    900
  42. Lie - Legend song by BIGBANG
    7,811
  43. Why ROLLBACK is useful when you work with Google Gemini CLI?
    832
  44. Reimbursement after Vaccination at McKinley Health Center
    1,015
  45. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    966
  46. Common Questions from UIUC school life in terms of CS Program
    1,117
  47. UIUC Immunization Compliance
    1,210
  48. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,089
  49. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,092
  50. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    864