Skip to main content

Oops Concepts : Inheritance In Java

When I first started learning Java, inheritance felt a bit confusing. But once I understood the basic idea, it became one of the easiest and most powerful concepts in Object-Oriented Programming (OOP).

In this blog, I’ll explain inheritance in very simple English, so even if you're a beginner student or aspiring developer, you can understand it easily. 

What is Inheritance in Java?

In simple words, inheritance means reusing code from another class.

I usually think of it like this:

  • A child inherits features from parents
  • Similarly, a class can inherit properties and methods from another class

Definition:

Inheritance is a mechanism where a child class gets properties and methods from a parent class.

Key Terms (Very Important)

Parent Class (Superclass) → The class that provides properties

Child Class (Subclass) → The class that inherits those properties

How Inheritance Works in Java

Java uses the keyword:

        extends

Basic Syntax:

This means the Child class can use everything from the Parent class.

Real Example (This Helped Me Understand)

In this example:

  • Animal is the parent class
  • Dog is the child class

What I learned from this:

  • The dog can use eat() from Animal
  • It can also use its own method bark()

This is called code reusability

Types of Inheritance in Java

1. Single Inheritance

This is the simplest type.

One child class inherits from one parent class

Example:

Parent → Child




2. Multilevel Inheritance

This works like a chain. Grandparent → Parent → Child


I found this useful when building layered applications.

3. Hierarchical Inheritance

Multiple child classes inherit from one parent
Parent
/ \
Child1 Child2



4. Multiple Inheritance (Using Interfaces)

Java does NOT support multiple inheritance using classes ❌ But it supports it using interfaces ✅


Advantages of Inheritance

From my experience, these are the biggest benefits:
  • Code Reusability → Write once, use many times
  • Less Code → No need to repeat logic
  • Easy Maintenance → Update in one place
  • Method Overriding → Customize behavior

Disadvantages of Inheritance

You should also know the downsides:
  • Can make code complex
  • Creates tight coupling
  • Harder to manage in large projects

Final Thoughts (My Advice)

When I started, I tried to learn everything at once—and got confused. What worked for me:
  • Start with simple examples
  • Practice small programs
  • Then move to advanced concepts

Comments

Popular posts from this blog

Resolving the “Script Execution Disabled” Error in PowerShell

  If you’ve encountered the error message: File C:\Program Files\nodejs\node_modules\npm\bin\npm.ps1 cannot be loaded because running scripts is disabled on this system. This guide will help you resolve the issue step-by-step Why Does This Error Occur? By default, PowerShell restricts the execution of scripts for security reasons. This restriction is controlled by the  Execution Policy , which determines which scripts can run on your system. To enable script execution, we need to adjust this policy. Step-by-Step Solution 1. Open PowerShell as Administrator Right-click on the   Start Menu   or press   Win + X . Select   Windows PowerShell (Admin) . This is necessary to make changes to your system’s settings. 2. Check the Current Execution Policy Run the following command to see the current policy for the current user: Get-ExecutionPolicy -Scope CurrentUser You will likely see   Restricted , which is the default policy. 3. Change the Execution Policy To ...

Stop Storing JWTs in LocalStorage: A 2026 Guide to MERN Auth

If you’re still putting your JSON Web Tokens (JWTs) into localStorage in your MERN apps, it's time to stop. As someone who’s spent way too many nights debugging broken auth flows and dealing with security audits, I’ve learned the hard way that localStorage is essentially an open invitation for XSS (Cross-Site Scripting) attacks to hijack your user sessions.  In 2026, the standard has shifted. Here is how we’re handling authentication in the MERN stack now. The Problem: LocalStorage is a Vulnerability  When you store a token in localStorage, any JavaScript running on your page—including a compromised third-party package or an injected script—can read that token. If a hacker manages to execute just one line of code in your app, they have your user's identity.    The Modern Shift: Cookies are Your Best Friend The gold standard now is to move tokens out of the reach of your client-side JavaScript by using HttpOnly, Secure, SameSite=Strict cookies. Because these cookies...