Inheritance is an object-oriented programming principle where a subclass inherits the attributes and behaviors of a superclass.

A superclass is a class that can be extended to create subclasses. It contains the attributes and behaviors that can be shared with subclasses.

A subclass is a class that extends a superclass and inherits its attributes and behaviors. It has the same attributes and behaviors as the superclass and can have additional ones of its own.

Inheritance creates an is-a relationship between the superclass and its subclasses.

  • A Dog is-a Pet
  • A Cat is-a Pet

Creating a New Class

To create a new class in Java Lab, we start by creating a new file.

We then enter the name of the class we want to create. Make sure the new file ends in .java!

In our new class, we write the class header.

We add the extends keyword to the class header to indicate that this is a subclass and the superclass that it inherits from.

Our Java program now consists of two types of classes. The class that represents an object and contains its attributes and behaviors. We can instantiate objects from this class.

The tester class, which is the class that contains the main method and from where the program starts running.