Java Inheritance (Subclass and Superclass)

JK JK 2030 
Created at
Updated at  
7,197 0 0

In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

  • subclass (child) - the class that inherits from another class
  • superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):

class Vehicle {
  protected String brand = "Ford";        // Vehicle attribute
  public void honk() {                    // Vehicle method
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle {
  private String modelName = "Mustang";    // Car attribute
  public static void main(String[] args) {

    // Create a myCar object
    Car myCar = new Car();

    // Call the honk() method (from the Vehicle class) on the myCar object
    myCar.honk();

    // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
    System.out.println(myCar.brand + " " + myCar.modelName);
  }
}

The final Keyword

If you don't want other classes to inherit from a class, use the final keyword:

final class Vehicle {
  ...
}

class Car extends Vehicle {
  ...
}

 

The output will be like below:

Main.java:9: error: cannot inherit from final Vehicle
class Main extends Vehicle {
                  ^
1 error)

 

Tags Java Java Inheritance Java SubClass Java SuperClass Facebook X
Comments 0
Similar posts
  1. Java Packages
    7,199
  2. Java Polymorphism
    7,076
  3. Java Classes and Objects
    7,125
  4. Java Inner Classes
    7,088
  5. Java Recursion
    7,215
  6. Java Scope
    7,129
  7. Java Methods
    7,115
  8. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,930
  9. Java Switch Statements
    7,433
  10. Java Short Hand If...Else (Ternary Operator)
    7,607
  11. Java If ... Else
    7,427
  12. Java Math
    7,461
  13. Java Variables
    7,501
  14. Java Comments
    8,075
  15. The Print() Method
    7,180
  16. Java Syntax
    7,479
  17. Java Getting Started
    7,431
  18. What is Java?
    7,118
  19. Machine Learning Types and Programming Languages
    7,277
  20. Java Tutorials associated with AP Computer Science A
    7,681
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,199
  22. Java Servlet Example
    7,143
  23. Difference between Java and Javascript
    7,668
  24. Challenge: One Code Problem Per Day
    1,163
  1. How do I replace content that based on the HTML UI Template
    7,179
  2. Dataset of California Foodbanks
    7,195
  3. Java Abstract Classes and Methods
    7,780
  4. Java Arrays
    8,801
Recently updated
  1. Michael Jackson's Billie Jean
    246
  2. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    13
  3. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    137
  4. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    140
  5. Watch This Dazzling Dancer’s Mind-Blowing Moves
    178
  6. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    220
  7. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    711
  8. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    340
  9. The Evolution and Production Reality of Agentic AI
    291
  10. How to Activate or Waive Your UIUC Student Health Insurance
    374
  11. Complete Guide to Building a Machine Learning Model
    408
  12. My life cuts at Las Vegas during Thanksgiving day holiday
    7,500
  13. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    429
  14. Harness vs. OpenClaw: Two Very Different "Agents"
    1,094
  15. Clean Python Environments: The Power of venv vs. Docker
    926
  16. What is Docker? Why is Docker also useful in a development environment?
    685
  17. UIUC 2026-2027 Academic Calendar
    1,740
  18. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    890
  19. Open-Source LLMs: The AI Revolution
    840
  20. Resume 2.0: Leveling Up for My First Software Gig
    2,456
  21. Not everyone will understand what this man just did
    2,045
  22. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,676
  23. Unpacking IU's Shopper
    851
  24. Jackie Chan's Police Story: The Action Masterpiece
    727
  25. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,034
  26. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,256
  27. Open Databases for Sex Crime Occurrences in the U.S.
    792
  28. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,868
  29. My First Day at University of Illinois-Urvana Champaign
    1,265
  30. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,235
  31. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,329
  32. Sipping the Stars: My Starbucks Adventure
    9,798
  33. Exciting explore at Sequoia National Park
    7,749
  34. My Life Shot at Death Valley
    1,818
  35. Ip Man fights with Muay Thai Master
    1,009
  36. Mad Clown - Don't Die
    1,111
  37. How to get Student Enrollment and Degree Verification at UIUC
    5,154
  38. LAX Thanksgiving Rush: A Joyful Reunion
    975
  39. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,205
  40. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,048
  41. 10CM - To Reach You (너에게 닿기를)
    1,224
  42. Feeling weak? Transform yourself at the UIUC ARC!
    1,632
  43. BOYNEXTDOOR - If I Say I Love You
    1,250
  44. The Future of Software Engineer - AI Engineering
    981
  45. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    984
  46. Lie - Legend song by BIGBANG
    7,889
  47. Why ROLLBACK is useful when you work with Google Gemini CLI?
    924
  48. Reimbursement after Vaccination at McKinley Health Center
    1,085
  49. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    1,025
  50. Common Questions from UIUC school life in terms of CS Program
    1,197