Зміст курсу
Java OOP
Java OOP
OOP 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:
Child
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:
- We create a class
Bird
, which is the parent class in our case. This class has its own fields and methods; - We create a class
Parrot
, which uses theextends
keyword to inherit from the parent classBird
. TheParrot
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 classBird
; - We create an object of the
Parrot
class in themain
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 classBird
, and then we initialize the fields of the child classParrot
. 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:
Parrot
@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:
Bird
Parrot
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.
Дякуємо за ваш відгук!