Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
OOP Principles: Inheritance | OOP
Java OOP
course content

Зміст курсу

Java OOP

Java OOP

1. How to Work With IDE?
2. OOP
3. Interface

bookOOP Principles: Inheritance

Inheritance

Let's talk about the principle of OOP - Inheritance.

Inheritance is a mechanism that allows the creation of classes based on another class.

  • The class that serves as the base is called the parent or superclass;
  • The class created based on the parent class is called the child, subclass, or descendant.

Thus, the child class can inherit the characteristics and properties of another class - the parent class (its methods and variables).

You can inherit a class using the keyword extends. The syntax for a child class inheriting from a parent class looks like this:

java

Child

copy
123
class Child extends Parent { // properties and methods }

Let's take a look at an example of how inheritance works in code. We have a class Bird with certain fields and methods, and there is a class Parrot with its own fields and methods. A parrot is a bird, so logically, the parrot should inherit from the Bird class:

Let's understand what is happening in these pictures:

  1. We create a class Bird, which is the parent class in our case. This class has its own fields and methods;
  2. We create a class Parrot, which uses the extends keyword to inherit from the parent class Bird. The Parrot class is a child class, and it has its own fields and methods. Additionally, as a child class, Parrot inherits all the fields and methods of the parent class Bird;
  3. We create an object of the Parrot class in the main method and see that we can initialize the fields of both the parent and the child classes. First, we initialize the fields of the parent class Bird, and then we initialize the fields of the child class Parrot. Next, we call the methods: first, the methods of the parent class, and then the method of the child class.

Thus, we used inheritance to inherit the Bird class.

Why do we do this?

When we have many different Bird subclasses, not just Parrot, for example, there are also Pigeon and Penguin. In that case, we will have a lot of duplicated fields, such as boolean canFly. The first thing inheritance helps is to avoid duplicated code fragments. Furthermore, it significantly improves readability. In practice, you rarely encounter the Bird class and its subclasses directly, but inheritance is commonly used, especially with interfaces.

Note

You will learn about interfaces in the next section of this course.

Inheritance rules

  • Inherit only one class. Java does not support multiple inheritance. One class can have only one parent;

  • Everything is inherited except private variables and methods.

Note

You cannot inherit from yourself!

Earlier, I mentioned that a subclass would have access to all variables and methods of the parent. However, that's not entirely true.

In reality, all methods and variables marked with the private modifier are not accessible to the subclass.

  • Override the parent class method.

Let's imagine that we inherit a class, but there are certain parts of the inherited code that we don't like. Suppose we want a specific method to work differently than in the parent class.

To override a method from the parent class, we write @Override above it:

java

Parrot

copy
1234
@Override public void eat() { System.out.println("The parrot is eating"); }

In the code above, we have overridden the method of the parent class, and now when this method is called from the child class, the overridden method will be invoked!

  • Preventing Inheritance.

If you don't want anyone to inherit your class, use the final modifier before it. For example:

java

Bird

java

Parrot

copy
123
public final class Bird { // fields and methods }

In this way, by using the keyword final, we have prohibited the inheritance of the Bird class.

These are just the fundamental rules and features of inheritance. It's a vast topic, and you will use it very frequently. Throughout the course, you will become familiar with other aspects of inheritance and will use them more often.

1. Why do we need inheritance in Java?
2. How many classes can we inherit from at once?
3. How can we prevent inheritance in Java?
Why do we need inheritance in Java?

Why do we need inheritance in Java?

Виберіть правильну відповідь

How many classes can we inherit from at once?

How many classes can we inherit from at once?

Виберіть правильну відповідь

How can we prevent inheritance in Java?

How can we prevent inheritance in Java?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt