Skip to main content

Multiple Inheritance of State and Implementation


Today, I was just curious about why an enum can not extend anything else. I took a look on the Oracle document here, and I found the answer is below:

"All enums implicitly extend java.lang.Enum. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else."

I have been learned of it before. But, wait a sec...! Why Java does not support multiple inheritance of state?

Since I have worked with other programming languages like C++, I was able to make a class extend some other classes.

The short answer is to avoid the issues of multiple inheritance of stateI wonder if other programming languages have these below terms but Java does.

Multiple inheritance of state

It is the ability to inherit fields from multiple classes. There is a problem and Java avoids it.

"For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field?"

Multiple inheritance of implementation

It is the ability to inherit method definitions from multiple classes.

There is a similar problem that sometimes compiler cannot determine which member or method to access or invoke. Java deals with that problem as below.

"As with multiple inheritance of implementation, a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. In this case, the compiler or the user must decide which one to use."

I just did a quick test for the above explaination

package vn.nvanhuong.java_lab;
//Inheritance from defined default methods
interface Foo{
default void method() {
System.out.println("Foo");
};
}
interface Bar{
default void method() {
System.out.println("Bar");
};
}
class MyClass implements Foo, Bar{
@Override
public void method() {
Foo.super.method(); //compiler forces to explicitly define this if inheritance
}
}
//Inheritance from defined static methods
class StaticFoo{
static void staticMethod() {
System.out.println("StaticFoo");
}
}
class StaticBar extends StaticFoo{
static void staticMethod() {
System.out.println("StaticBar");
}
}
class MyStaticClass extends StaticBar{
static void myStaticMethod() {
staticMethod(); //default -> uses from direct parent
StaticFoo.staticMethod(); //users must explicitly defines if inheritance
}
}
//Result
public class MultiInheritance {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.method();
//Foo
MyStaticClass.myStaticMethod();
//StaticBar
//StaticFoo
}
}
Conclusion, Java supports multiple inheritance of implementation but multiple inheritance of state.

Reference
[1]. https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
[2]. https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html

Comments

Popular posts from this blog

[Snippet] CSS - Child element overlap parent

I searched from somewhere and found that a lot of people says a basic concept for implementing this feature looks like below: HTML code: <div id="parent">  <div id="child">  </div> </div> And, CSS: #parent{   position: relative;   overflow:hidden; } #child{   position: absolute;   top: -1;   right: -1px; } However, I had a lot of grand-parents in my case and the above code didn't work. Therefore, I needed an alternative. I presumed that my app uses Boostrap and AngularJs, maybe some CSS from them affects mine. I didn't know exactly the problem, but I believed when all CSS is loaded into my browser, I could completely handle it. www.tom-collinson.com I tried to create an example to investigated this problem by Fiddle . Accidentally, I just changed: position: parent; to position: static; for one of parents -> the problem is solved. Look at my code: <div class="modal-body dn-placeholder-parent-positi...

Coding Exercise, Episode 1

I have received the following exercise from an interviewer, he didn't give the name of the problem. Honestly, I have no idea how to solve this problem even I have tried to read it three times before. Since I used to be a person who always tells myself "I am not the one good at algorithms", but giving up something too soon which I feel that I didn't spend enough effort to overcome is not my way. Then, I have sticked on it for 24 hours. According to the given image on the problem, I tried to get more clues by searching. Thanks to Google, I found a similar problem on Hackerrank (attached link below). My target here was trying my best to just understand the problem and was trying to solve it accordingly by the Editorial on Hackerrank. Due to this circumstance, it turns me to love solving algorithms from now on (laugh). Check it out! Problem You are given a very organized square of size N (1-based index) and a list of S commands The i th command will follow t...

DevOps for Dummies

Everyone talks about it, but not everyone knows what it is. Why DevOps? In general, whenever an organization adopts any new technology, methodology, or approach, that adoption has to be driven by a business need. Any kind of system that need rapid delivery of innovation requires DevOps (development and operations). Why? DevOps requires mechanisms to get fast feedback from all the stakeholders in the software application that's being delivered. DevOps approaches to reduce waste and rework and to shift resources to higher-value activities. DevOps aims to deliver value (of organization or project) faster and more efficiently. DevOps Capabilities The capabilities that make up DevOps are a broad set that span the software delivery life cycle. The following picture is a reference architecture which provides a template of a proven solution by using a set of preferred methods and capabilities. My Remarks Okay, that sounds cool. What does it simply mean, again? The f...

How did I start practising BDD?

In the beginning days, I have practiced TDD (Test Driven Development) using JUnit, I approached that I should test methods belong to a class. For example: I have a class with some methods: public class A{ public void method1(){ } public void method2(){ } } And then, I wrote some test methods to check the corresponding ones, for example: public class ATest{ @Test public void testMethod1(){ .... assertTrue(...); ..... assertEquals(...); } @Test public void testMethod2(){ } } After that, I know that a test method (ex: testMethod1) should just only test one thing, so I decided to write more methods for each case. It looks like the following: @Test public void testMethod1_When_Case1(){ .... assertTrue(...); } @Test public void testMethod1_When_Case2(){ .... assertEquals(...); } However, it was not a really good approach because it seems that I just focused on test the functionality of the method of the class. With the TDD approach, I knew that I s...

Selenium - Override javascript functions to ignore downloading process

I have got an issue with downloading process on IE 8. This browser blocks my automatic-download functionality on my app so that I could not work with my test case any more after that. In my case, I didn't care about the file is downloaded or not, I just focus on the result after downloading process finished successfully. Therefore, I found a solution to ignore this process so that I can work normally. I use Primefaces, here is the remote command to trigger the download process <p:remoteCommand name="cmdGenerateDocument" actionListener="#{logic.onGenerateDocument}" update="xrflDocumentCreationPanel" oncomplete="clickDownloadButton();"/> The following is my test case: @Test public void shouldUpdateStep6ToWarningIfStep1IsValidAfterFinished(){ MainPage mainPage = new MainPage(); waitForLoading(mainPage.getDriver()); EmployeeDetailPage empDetailPage = new EmployeeDetailPage(); waitForLoading(empDetailPage.getDriver()); e...