Java Inheritance (Subclass and Superclass)

JK 2010 
Created at
Updated at  
7,163 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,159
  2. Java Polymorphism
    7,036
  3. Java Classes and Objects
    7,061
  4. Java Inner Classes
    7,027
  5. Java Recursion
    7,182
  6. Java Scope
    7,074
  7. Java Methods
    7,062
  8. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,814
  9. Java Switch Statements
    7,379
  10. Java Short Hand If...Else (Ternary Operator)
    7,546
  11. Java If ... Else
    7,368
  12. Java Math
    7,409
  13. Java Variables
    7,426
  14. Java Comments
    7,965
  15. The Print() Method
    7,136
  16. Java Syntax
    7,413
  17. Java Getting Started
    7,383
  18. What is Java?
    7,074
  19. Machine Learning Types and Programming Languages
    7,214
  20. Java Tutorials associated with AP Computer Science A
    7,562
  21. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,150
  22. Java Servlet Example
    7,102
  23. Difference between Java and Javascript
    7,574
  24. Challenge: One Code Problem Per Day
    1,049
  1. Java Servlet Example
    7,102
  2. How do I replace content that based on the HTML UI Template
    7,138
  3. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,150
  4. Dataset of California Foodbanks
    7,152
  5. Java Tutorials associated with AP Computer Science A
    7,562
  6. Java Inner Classes
    7,027
  7. Java Polymorphism
    7,036
  8. Java Packages
    7,159
  9. Java Abstract Classes and Methods
    7,676
  10. Java Classes and Objects
    7,061
  11. Java Recursion
    7,182
  12. Java Scope
    7,074
  13. Java Methods
    7,062
  14. Java Arrays
    8,628
  15. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,814
  16. Java Switch Statements
    7,379
  17. Java Short Hand If...Else (Ternary Operator)
    7,546
  18. Java If ... Else
    7,368
  19. Java Math
    7,409
  20. Java Variables
    7,426
  21. Java Comments
    7,965
  22. The Print() Method
    7,136
Recently updated
  1. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    84
  2. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    209
  3. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    182
  4. The Evolution and Production Reality of Agentic AI
    174
  5. How to Activate or Waive Your UIUC Student Health Insurance
    253
  6. Complete Guide to Building a Machine Learning Model
    295
  7. My life cuts at Las Vegas during Thanksgiving day holiday
    7,376
  8. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    319
  9. Harness vs. OpenClaw: Two Very Different "Agents"
    912
  10. Clean Python Environments: The Power of venv vs. Docker
    785
  11. What is Docker? Why is Docker also useful in a development environment?
    591
  12. UIUC 2026-2027 Academic Calendar
    1,505
  13. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    771
  14. Open-Source LLMs: The AI Revolution
    726
  15. Resume 2.0: Leveling Up for My First Software Gig
    2,116
  16. Not everyone will understand what this man just did
    1,748
  17. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,554
  18. Unpacking IU's Shopper
    719
  19. Jackie Chan's Police Story: The Action Masterpiece
    616
  20. The IVE Story: Identity, 'I AM' Charts, and Influence
    916
  21. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,154
  22. Open Databases for Sex Crime Occurrences in the U.S.
    687
  23. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,547
  24. My First Day at University of Illinois-Urvana Champaign
    1,161
  25. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,094
  26. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,171
  27. Sipping the Stars: My Starbucks Adventure
    9,593
  28. Exciting explore at Sequoia National Park
    7,624
  29. My Life Shot at Death Valley
    1,648
  30. Ip Man fights with Muay Thai Master
    910
  31. Mad Clown - Don't Die
    982
  32. How to get Student Enrollment and Degree Verification at UIUC
    4,689
  33. LAX Thanksgiving Rush: A Joyful Reunion
    901
  34. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,087
  35. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    952
  36. 10CM - To Reach You (너에게 닿기를)
    1,133
  37. Feeling weak? Transform yourself at the UIUC ARC!
    1,551
  38. BOYNEXTDOOR - If I Say I Love You
    1,163
  39. The Future of Software Engineer - AI Engineering
    915
  40. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    886
  41. Lie - Legend song by BIGBANG
    7,790
  42. Why ROLLBACK is useful when you work with Google Gemini CLI?
    808
  43. Reimbursement after Vaccination at McKinley Health Center
    986
  44. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    942
  45. Common Questions from UIUC school life in terms of CS Program
    1,084
  46. UIUC Immunization Compliance
    1,176
  47. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,071
  48. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,072
  49. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    848
  50. Common methods to improve coding skills
    969