CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Subclass and Superclass in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Subclass and Superclass in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

In Java, a subclass is a class that inherits from a superclass. The subclass is able to access and reuse the fields and methods of the superclass, and can also add new fields and methods of its own. This is a powerful feature of object-oriented programming, as it allows developers to create hierarchical relationships between classes and build upon existing functionality.

To create a subclass in Java, we use the extends keyword, followed by the name of the superclass. For example:

public class Dog extends Animal {
  // fields and methods for Dog class go here
}

In this example, the Dog class is a subclass of the Animal class. The Dog class has access to all of the fields and methods of the Animal class, as well as any fields and methods that it defines itself.

It’s important to note that the subclass is a more specific type of the superclass. In other words, a Dog is an Animal, but an Animal is not necessarily a Dog. This is known as an “is-a” relationship.

One common use case for subclassing is to override the methods of the superclass. For example, we might want to define a new behavior for the eat() method for the Dog class, but keep the same behavior for other subclasses of Animal. To do this, we can use the @Override annotation and provide a new implementation of the method in the subclass:

public class Dog extends Animal {
  @Override
  public void eat() {
    // new implementation of eat() method for Dog class
  }
}

Another way to use subclassing is to create a subclass that adds new functionality to the superclass. For example, we might want to create a subclass of Animal called FlyingAnimal, which adds a new fly() method to the Animal class. To do this, we can define the fly() method in the FlyingAnimal subclass:

public class FlyingAnimal extends Animal {
  public void fly() {
    // implementation of fly() method goes here
  }
}

In summary, subclassing is a way to create a new class that is based on an existing class, and to add or modify the functionality of that existing class. It allows developers to build upon existing code and create more specialized classes that can be used in a variety of different contexts.

Related Posts:

  • Inheritance in Java: A Developer’s Guide to Code Reusability
  • Extends Keyword in Java: A Deep Dive into Inheritance
  • final Keyword in Java
  • Abstract Class in Java: Bridging Code Reusability…
  • Packages in Java: A Guide to Modular, Maintainable Code
  • Abstraction in Java
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleJDBC Interview Questions: Ace Your Technical Screening
Next Article →super Keyword in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap