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

Зміст курсу

Java OOP

OOP Principles: PolymorphismOOP Principles: Polymorphism

Polymorphism

Polymorphism is another principle of OOP. You have already encountered polymorphism when you overloaded and overridden methods. In general, this is the essence of polymorphism. But the definition of polymorphism can be a bit intimidating:

Polymorphism (many forms) is the ability to use methods with the same name but different parameter sets in one or a group of classes that are connected by inheritance. It is also the ability to modify the methods of a parent class for child classes.

But in reality, it's much simpler than it seems. Polymorphism, in simple terms, can be divided into 2 parts:

  • Method Overloading: what you learned in this chapter, but let's review.
Method overloading is creating a method with the same name but different parameters and/or return type.

Let's look at an example: we need to write a method that takes an int parameter and returns a String, as well as a method that does the same but with a long parameter. Let's take a look at the code snippet:

java

Main.java

java

Present😏.java

As you can see above, we've created 2 methods with the same name but can do different things. This is method overloading.

  • Method Overriding: You've encountered this topic before when you overridden the toString method in this chapter. But let's review it once again.
Method overriding is the modification of a method from the parent class in the child class. You can override a method by placing the @Override annotation above the method in the child class.

Let's look at a code snippet that will show us how we can override a method. We have a class called Airplane that inherits from the Transport class. And in the Transport class, there's a method called move which returns "This transport has started moving".

We need the airplane to "start flying" instead of "moving". To achieve this, we will override the move method in the child class:

java

Transport.java

java

Airplane.java

As you can see, we have overridden the method from the parent class in the child class as required.

In this way, polymorphism complements inheritance very well. Through polymorphism, we can conveniently and optimally extend our code, making it flexible.

1. Why do we need polymorphism in Java?
2. How does polymorphism complement inheritance?
3. What keyword is used to overload a method?

Why do we need polymorphism in Java?

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

How does polymorphism complement inheritance?

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

What keyword is used to overload a method?

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

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

Секція 2. Розділ 4
course content

Зміст курсу

Java OOP

OOP Principles: PolymorphismOOP Principles: Polymorphism

Polymorphism

Polymorphism is another principle of OOP. You have already encountered polymorphism when you overloaded and overridden methods. In general, this is the essence of polymorphism. But the definition of polymorphism can be a bit intimidating:

Polymorphism (many forms) is the ability to use methods with the same name but different parameter sets in one or a group of classes that are connected by inheritance. It is also the ability to modify the methods of a parent class for child classes.

But in reality, it's much simpler than it seems. Polymorphism, in simple terms, can be divided into 2 parts:

  • Method Overloading: what you learned in this chapter, but let's review.
Method overloading is creating a method with the same name but different parameters and/or return type.

Let's look at an example: we need to write a method that takes an int parameter and returns a String, as well as a method that does the same but with a long parameter. Let's take a look at the code snippet:

java

Main.java

java

Present😏.java

As you can see above, we've created 2 methods with the same name but can do different things. This is method overloading.

  • Method Overriding: You've encountered this topic before when you overridden the toString method in this chapter. But let's review it once again.
Method overriding is the modification of a method from the parent class in the child class. You can override a method by placing the @Override annotation above the method in the child class.

Let's look at a code snippet that will show us how we can override a method. We have a class called Airplane that inherits from the Transport class. And in the Transport class, there's a method called move which returns "This transport has started moving".

We need the airplane to "start flying" instead of "moving". To achieve this, we will override the move method in the child class:

java

Transport.java

java

Airplane.java

As you can see, we have overridden the method from the parent class in the child class as required.

In this way, polymorphism complements inheritance very well. Through polymorphism, we can conveniently and optimally extend our code, making it flexible.

1. Why do we need polymorphism in Java?
2. How does polymorphism complement inheritance?
3. What keyword is used to overload a method?

Why do we need polymorphism in Java?

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

How does polymorphism complement inheritance?

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

What keyword is used to overload a method?

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

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

Секція 2. Розділ 4
some-alt