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.java
123class 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:
Here's the code from the screenshot, rewritten for better readability and clarity:
Bird.java
Parrot.java
Main.java
12345678910111213141516171819package animals.birds; public class Bird { public String color; public boolean canFly; public int maxAge; public void eat() { System.out.println("The bird is eating"); } public void fly() { if (canFly) { System.out.println("The bird is flying!"); } else { System.out.println("This bird can't fly :("); } } }
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 theextendskeyword to inherit from the parent classBird. TheParrotclass is a child class, and it has its own fields and methods. Additionally, as a child class,Parrotinherits all the fields and methods of the parent classBird; - We create an object of the
Parrotclass in themainmethod 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
privatevariables 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.java
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:
Bird.java
Parrot.java
123public 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?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Ask me questions about this topic
Summarize this chapter
Show real-world examples
Awesome!
Completion rate improved to 4.76
OOP Principles: Inheritance
Swipe to show menu
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.java
123class 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:
Here's the code from the screenshot, rewritten for better readability and clarity:
Bird.java
Parrot.java
Main.java
12345678910111213141516171819package animals.birds; public class Bird { public String color; public boolean canFly; public int maxAge; public void eat() { System.out.println("The bird is eating"); } public void fly() { if (canFly) { System.out.println("The bird is flying!"); } else { System.out.println("This bird can't fly :("); } } }
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 theextendskeyword to inherit from the parent classBird. TheParrotclass is a child class, and it has its own fields and methods. Additionally, as a child class,Parrotinherits all the fields and methods of the parent classBird; - We create an object of the
Parrotclass in themainmethod 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
privatevariables 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.java
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:
Bird.java
Parrot.java
123public 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?
Thanks for your feedback!