Java Challengers # 3: Polymorphism and inheritance
Java Challengers # 3: Polymorphism and inheritance
3r3r7787. 3r33775.
We continue to translate a series of articles with puzzles on Java. The last post about lines astonished 3r312. heated debate
. We hope that you will not pass by this article either. And yes - we now invite the tenth anniversary stream of our course 3r314. Java Developer 3r3779. .
3r33780.
3r3r7787. 3r33775. According to the legendary Venkatu Subramaniam (Venkat Subramaniam) polymorphism is the most important concept in object - oriented programming.
Polymorphism
- or the ability of an object to perform specialized actions based on its type — this is what makes Java code flexible. Design patterns, such as Command (Command), Observer, Decorator, Strategy, and many others created by a gang of four, all use some form of polymorphism. Mastering this concept will greatly improve your ability to think through software solutions. 3r33780.
3r3r7787. 3r33775. 3r33780.
3r3r7787. 3r33775. You can get the source code for this article and experiment here: 3r3336. https://github.com/rafadelnero/javaworld-challengers
3r33780.
3r3r7787.
Interfaces and inheritance in polymorphism
3r3r7787. 3r33775. In this article, we will focus on the relationship between polymorphism and inheritance. The main thing to keep in mind is that polymorphism requires
inheritance 3r33253. or
interface implementations 3r33253. . You can see this in the example below with Duke (3r3708. Duke ) And Juggy (3r3708. Juggy ):
3r3r7787. 3r33737. 3r3667. public abstract class JavaMascot {
public abstract void executeAction (); 3r3r7787.}
3r3r7787. public class Duke extends JavaMascot {
@Override
public void executeAction () {
System.out.println ("Punch!"); 3r3r7787.}
}
3r3r7787. public class Juggy extends JavaMascot {
@Override
public void executeAction () {
System.out.println ("Fly!"); 3r3r7787.}
}
3r3r7787. public class JavaMascotTest {
public static void main (String args) {
JavaMascot dukeMascot = new Duke (); 3r3r7787. JavaMascot juggyMascot = new Juggy (); 3r3r7787. dukeMascot.executeAction (); 3r3r7787. juggyMascot.executeAction (); 3r3r7787.}
}
3r3r7787. 3r33775. The output of this code will be as follows:
3r3r7787. 3r33737. 3r3667. Punch! 3r3r7787. Fly!
3r3r7787. 3r33775. As the specific implementations are defined, the methods and 3r3708 will be invoked. Duke and 3r3708. Juggy . 3r33780.
3r3r7787. 3r33775. Overloading (overloading) method - is it polymorphism? Many programmers confuse polymorphism relationship with overriding methods and overloading methods . In fact, only redefinition of the method is true polymorphism. Overload uses the same method name, but different parameters. Polymorphism is a broad term, so there will always be discussions on this topic. 3r33780.
3r3r7787. 3r3113. What is the purpose of polymorphism
3r3r7787. 3r33775. The big advantage and purpose of using polymorphism is to reduce the client class relatedness with the implementation. Instead of hardcoding, the client class gets a dependency implementation to perform the necessary action. Thus, the client class knows a minimum to perform its actions, which is an example of weak binding. 3r33780.
3r3r7787. 3r33775. To better understand the goal of polymorphism, take a look at SweetCreator
:
3r3r7787. 3r33737. 3r3667. public abstract class SweetProducer {
public abstract void produceSweet (); 3r3r7787.}
3r3r7787. public class CakeProducer extends SweetProducer {
@Override
public void produceSweet () {
System.out.println ("Cake produced"); 3r3r7787.}
}
public class ChocolateProducer extends SweetProducer {
@Override
public void produceSweet () {
System.out.println ("Chocolate produced"); 3r3r7787.}
}
public class CookieProducer extends SweetProducer {
@Override
public void produceSweet () {
System.out.println ("Cookie produced"); 3r3r7787.}
}
3r3r7787. public class SweetCreator {
private List sweetProducer; 3r3r7787. 3r3r7787. public SweetCreator (List sweetProducer) {
this.sweetProducer = sweetProducer; 3r3r7787.}
3r3r7787. public void createSweets () {
sweetProducer.forEach (sweet -> sweet.produceSweet ()); 3r3r7787.}
}
3r3r7787. public class SweetCreatorTest {
public static void main (String args) {
SweetCreator sweetCreator = new SweetCreator (Arrays.asList (
New CakeProducer (),
New ChocolateProducer (),
New CookieProducer ())); 3r3r7787. 3r3r7787. sweetCreator.createSweets (); 3r3r7787.}
}
3r3r7787. 3r33775. In this example, you can see that the class is SweetCreator
knows only about the class SweetProducer
. He does not know the implementation of each Sweet
. This separation gives us the flexibility to update and reuse our classes, and this makes the code much easier to maintain. When designing a code, always look for ways to make it as flexible and convenient as possible. Polymorphism is a very powerful way to use for this purpose. 3r33780.
3r3r7787. 3r3189. Annotation @Override
obliges the programmer to use the same method signature that needs to be redefined. If the method is not overridden, there will be a compilation error. 3r3192.
3r3r7787. 3r3195. Covariant return types when overriding the method
3r3r7787. 3r33775. You can change the return type of the overridden method if it is 3r3200. covariant type . The covariant type is basically a subclass of the return value. 3r33780.
3r3r7787. 3r33775. Consider an example:
3r3r7787. 3r33737. 3r3667. public abstract class JavaMascot {
abstract JavaMascot getMascot (); 3r3r7787.}
3r3r7787. public class Duke extends JavaMascot {
@Override
Duke getMascot () {
return new Duke (); 3r3r7787.}
}
3r3r7787. 3r33775. Since Duke
is JavaMascot
, we can change the type of the return value when overriding. 3r33780.
3r3r7787. 3r33232. Polymorphism in Java base classes.
3r3r7787. 3r33775. We constantly use polymorphism in the base Java classes. One very simple example is creating an instance of the class ArrayList
with a type declaration as an interface List
. 3r33780.
3r3r7787. 3r33737. 3r3667. List list = new ArrayList <>();
3r3r7787. 3r33775. Consider sample code using the Java Collections API 3r33232. without polymorphism:
3r3r7787. 3r33737. 3r3667. public class ListActionWithoutPolymorphism {
//An example without polymorphism
void executeVectorActions (Vector
It may be interesting
weber
Author7-12-2018, 03:36
Publication DateJavaScript / Programming
Category- Comments: 0
- Views: 331
Helpful information. Fortunate me I discovered your web site accidentally,
and I am stunned why this accident did not happen earlier! I bookmarked it. Thanks, I've recently been looking for information about this topic for [hide]a[https://www.pizzahutcouponcode.com/pizza-hut-coupons-code/
] long time and yours is the greatest I've discovered so far. But, what concerning the conclusion? Are you positive about the source?
entegrasyon programları
entegrasyon programları