JavaRush /Java Blog /Random EN /Coffee break #95. How to solve the multiple inheritance p...

Coffee break #95. How to solve the multiple inheritance problem in Java

Published in the Random EN group
Source: FreeCodeCamp Java is one of the most popular object-oriented programming languages ​​used today. Since it is platform independent, you can find Java applications on every type of device and every operating system. And because Java is relatively easy to learn, it is one of the languages ​​that many programmers master. An important feature of Java that you should be familiar with is class inheritance. Inheritance allows you to optimize your code, making it easier to reuse classes. When you can reuse code that has already been tested and debugged, the software development life cycle becomes shorter and less expensive. Coffee break #95.  How to solve the multiple inheritance problem in Java - 1Although a simple concept in theory, coding inheritance relationships requires attention to detail. Especially with regard to multiple inheritance, where one child class inherits properties from multiple parent classes. Java rejects multiple inheritance relationships because they create ambiguity, but there are several ways to achieve many of the same effects if you know what to do. In this article, we'll look at issues with multiple inheritance and discuss alternative coding options in Java.

Inheritance terminology

Sometimes, to become a successful programmer, you need to become a problem solver to find workarounds for common errors or problems. This is an essential part of secure and smart coding. One such problem is related to multiple inheritance (or rather, lack thereof) in Java. Coffee break #95.  How to solve the multiple inheritance problem in Java - 2To fully understand inheritance in Java, you need to be familiar with the basic inheritance terminology of object-oriented programming (OOP).
  • Classes are the fundamental pattern structure in object-oriented programming languages. A class defines common properties for a group of objects.
  • Parent class : Also known as base classes or super classes. A parent class is an extensible class that provides functionality to a child class. It allows for reuse. The definitions and functions of the parent class are reused when creating child classes.
  • Child class : More generically called a subclass, a child class inherits functionality from another class. Child classes are extended or derived classes.
  • Inheritance : The relationship between parent and child classes.

OOP inheritance types

There are many popular object-oriented programming languages ​​in use today, including Java, C++, JavaScript, Python, PHP, Ruby, and Perl. Although inheritance is a common concept among these OOP languages, not all types of inheritance exist in each of these languages. It is extremely important to know the common types of inheritance and the restrictions on inheritance in the specific language you are using. The more you know about inheritance, the more effective developer you will become. Types of inheritance supported by Java include:
  • Single-level inheritance : When a child class inherits features from a single parent class.
  • Multi-level inheritance : This is a multi-level form of single-level inheritance. In multi-level inheritance, a child class can also act as a parent class for other child classes. The relationship between each level is linear - no branches go higher than with multiple inheritance. In this case, the final child class has functions from all levels above.
  • Hierarchical inheritance : The opposite of multiple inheritance. In hierarchical inheritance, a single parent class has more than one child class. So instead of having branches above it, it branches out below.
  • Hybrid Inheritance : As the name suggests, hybrid inheritance is a combination of other types of inheritance.
In addition to the inheritance types listed above, there are other types that Java does not support.
  • Multiple inheritance : In multiple inheritance, a child class has more than one parent class. Although Java and JavaScript do not support multiple inheritance, OOP languages ​​such as C++ do.
  • Multi-path inheritance : A hybrid of multiple, multi-level and hierarchical inheritance, in multi-path inheritance, a child class inherits its characteristics and functions from the parent class and several child classes of the parent class. Since multipath inheritance is based on multiple inheritance, Java does not support its use.

Why Java doesn't support multiple inheritance

The main problem with multiple inheritance is that it can create ambiguities in child classes. In a 1995 white paper, lead Java designer James Gosling stated that problems with multiple inheritance were one of the reasons Java was created. The complexities inherent in multiple inheritance are most clearly seen in the diamond problem. In the diamond problem, the parent class A has two distinct child classes B and C; that is, child classes B and C extend class A. Coffee break #95.  How to solve the multiple inheritance problem in Java - 3Now we create a new child class D that extends both class B and class C. Note that we have multiple inheritance (D extends B and C), hierarchical inheritance ( B and C extend A) and multi-level inheritance (D extends A, B and C). In the diamond problem, child classes B and C inherit a method from parent class A. Both B and C override the inherited method. But the new methods in B and C contradict each other. The final child class D inherits two independent and conflicting methods from its multiple parents B and C. It is not clear which method of class D should be used, so ambiguity arises. Other OOP programming languages ​​implement different methods for resolving the ambiguity of multiple inheritance.

How to solve the multiple inheritance problem in Java

Just because multiple inheritance is problematic doesn't mean it's useless. There are many situations where you might want one class to have functions from several other classes. Just think about the Tesla Roadster you'll buy when you become an extremely successful software developer. Its technical characteristics are based on both the sports car class and the electric vehicle class. Another example: the browser through which you are reading this article. It has features from the Internet privacy solutions class and from the general Internet browser class. But you can't extend multiple classes in Java. So how does this language deal with the problem of multiple inheritance? Java uses structures called interfaces. Interfaces are abstract types that define behavior to be implemented by classes. Because they are abstract, interfaces do not contain detailed instructions on their behavior. Instead, classes provide concrete implementations of interface behavior. Interfaces have several defining characteristics:
  • Unlike classes, you don't instantiate interfaces. Instead, classes implement interfaces.
  • Interfaces contain only public constant definitions and method headers.
  • Interfaces can only extend other interfaces, not classes.
  • Interfaces can extend multiple interfaces, and classes can implement multiple interfaces.
We can now effectively work around the diamond problem using interfaces. Remembering that only interfaces can extend only other interfaces, and any class that requires multiple inheritance characteristics must implement multiple interfaces, we can override diamond problem classes. What used to be classes A, B, and C now become interfaces A, B, and C. Interfaces B and C still extend interface A, but none of these interfaces have specific functionality, only specific behaviors. Class D remains the class that is responsible for specifically implementing the behavior found in interfaces B and C. Note one key difference: class D does not extend interfaces B and C. Instead, it implements them. This way you don't actually have multiple inheritance. Instead, you simply reframed the problem.

Conclusion

Understanding inheritance is essential for any effective Java developer. It's also equally important to know the limitations of inheritance and Java's built-in workaround for traditional multiple inheritance problems. Learning how to create interfaces to recreate the effects of multiple inheritance in Java will improve your productivity and hiring opportunities.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION