CJCoding With Joseph
15free / daySign inCreate account

Mastering Java - Full Guide

Understand Inheritance

Everything On Classes

Now that you understand how to build and use your own classes in Java, it’s time to go one step further.

Let’s say we want to make a new kind of bank account. Maybe this account earns interest. Or maybe it has special rules. We could just make a whole new class from scratch, but we don’t want to copy and paste all the same fields (like name, accountNumber, and balance). That’s where inheritance comes in.

What Is Inheritance?

Inheritance is a way to create a new class that inherits the properties and methods from an existing class.

  • The original class is called the superclass (or parent class).

  • The new class is called the subclass (or child class).

We use inheritance when we want the subclass to have everything the superclass has plus some extra things or changes.

Let’s say we want to build a new type of bank account that earns interest. We'll call it SavingsAccount. It should still have a name, account number, and balance so we can just reuse the BankAccount class and extend it.

Members only

Keep reading this lesson

You've reached the end of the free preview. Unlock the rest of this lesson plus 14 more lessons in Mastering Java - Full Guide.

  • Unlimited coding questions
  • Unlimited access to all classroom material
  • Weekly live call for questions and guidance
  • 3,000 AI tutor tokens / month
  • Personal SQL database (3 MB)
  • Progress tracking

Cancel any time.