Post by account_disabled on Dec 6, 2023 3:27:30 GMT -5
Especially the last option inheritance allows us to easily describe many similar entities in a simple way we declare the part that is common to them only once in the parent class while the variable part is placed in the code of subclasses inheriting from it. It turns out however that both in everyday life inheriting debts from a distant relative that we had no idea about! and in programming inheritance can cause us many unexpected difficulties.
What's the problem? To explain this we will first use a drawing relating to the principle that we will discuss later in the article. We will then turn the drawing into a simple application composed of several classes. Let's look at the two types Email Marketing List of ducks below . jpro graphic From ducks to delegations or problems with inheritance in Java Figure Bad use of inheritance using ducks as an example Now let's try to write the above image using Java classes. Theoretically two classes should be enough for this one representing a live duck and the other an electric duck. A live duck can swim and quack . code From ducks to delegations or problems with inheritance in Java Listing Duck Class However an electric duck behaves just like a living duck and therefore inherits from it.
Unless of course someone remembered and put batteries in it! Otherwise it responds with unexpected exceptions code From ducks to delegations or problems with inheritance in Java Listing ElectricDuck class Now since we are in the st century instead of creating a class with a main method let's write a test that will check the correct operation of the Duck class . code From ducks to delegations or problems with inheritance in Java Listing DuckTest class We deliberately parameterized the created test so that it could be passed various objects of the Duck class to it.
What's the problem? To explain this we will first use a drawing relating to the principle that we will discuss later in the article. We will then turn the drawing into a simple application composed of several classes. Let's look at the two types Email Marketing List of ducks below . jpro graphic From ducks to delegations or problems with inheritance in Java Figure Bad use of inheritance using ducks as an example Now let's try to write the above image using Java classes. Theoretically two classes should be enough for this one representing a live duck and the other an electric duck. A live duck can swim and quack . code From ducks to delegations or problems with inheritance in Java Listing Duck Class However an electric duck behaves just like a living duck and therefore inherits from it.
Unless of course someone remembered and put batteries in it! Otherwise it responds with unexpected exceptions code From ducks to delegations or problems with inheritance in Java Listing ElectricDuck class Now since we are in the st century instead of creating a class with a main method let's write a test that will check the correct operation of the Duck class . code From ducks to delegations or problems with inheritance in Java Listing DuckTest class We deliberately parameterized the created test so that it could be passed various objects of the Duck class to it.