Java Fundamentals

Multiple Inheritance

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "Multiple Inheritance" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates how classes achieve multiple inheritance by implementing multiple interfaces. An example of an interface method using the default type is also covered in this segment.

Preview
Close

Transcript from the "Multiple Inheritance" Lesson

[00:00:00]
>> A class we talked about, it can only extend one other class, but it can implement multiple interfaces. So this is how you might achieve multiple inheritance in Java. Besides Inheriting from a superclass that inherits from another superclass, you could implement multiple interfaces. And that way you get all of its abstract methods that you would then implement.

[00:00:34]
Now, when you're implementing multiple interfaces, the class header must specify each of those interfaces in this comma delimited list. So in this case, we have a class called electronic book, and it's implementing two interfaces, product and digital file. So that would be a comma delimited list. And then all of the abstract methods that exist in both of those classes are going to be in this class would need to be implemented in this class.

[00:01:09]
If a class declares that it's gonna implement multiple interfaces, it must, again, provide the abstract method. So this one, electronic book, I have to implement all methods from product and all abstract methods from digital file or I call myself abstract as well. If more than one of the interfaces that being implemented contains a method with the same signature, like look at this one product has getSize and no parameters and so does DigitalFile, so that's two.

[00:01:53]
You've inherited two of these methods that have the exact same signature. What you can do in the implementing class in electronic book is just implement it one time and that will satisfy that requirement. So you wouldn't add two methods in your electronic book class. Now if there are multiple interfaces being implemented and two or more of them contain a method with the same name but a different return type.

[00:02:27]
The class is going to have an error when attempting to implement them because there is a clash here. So even though I said I'm doing getSize, the one that returns a string, it's an error, who can tell why that's an error?
>> Because the return type isn't necessarily the same thing as a signature so it doesn't know what to look for

[00:02:58]
>> Exactly the return type is not a part of a method signature. The method signature is the name in the parameter list. So technically, both of those methods have the same signature, right? And now Java is smart enough to realize, wait, they have the same signature but they have a different return type, this is a clash, you just can't do it.

[00:03:27]
Right, in addition to abstract methods, interfaces can also contain default methods as well as static methods. So unlike abstract methods, both of these types of methods do provide implementation. There was a point in time when interfaces only contain abstract methods, like that was the definition of our interface was, it's a special kind of class and only contains abstract methods.

[00:04:02]
But that has since changed as the language has evolved. And they allow two other kinds of methods now static, which we talked about quite a bit, right? Who remembers what's a static method?
>> Methods that are available without an instance.
>> Okay, there we go and then default, we have not talked about just yet, but I wanna show you what that means with an example.

[00:04:29]
So let's go ahead and revisit our product interface. Let's say that after we've already produced this code, it's already out in production, there's already classes that are implementing it. We realize we wanna update this interface, right? And we say yeah, every product also has a price. So we wanna add those relative methods to the product interface.

[00:05:03]
So let's go back to product. And we're gonna add a couple of methods for price. So we'll say double getPrice and void setPrice, Okay, so we have a problem here, right? Again, by default, these methods are implicitly abstract, they're implicitly public. Which means in the book class we now have two methods that have not been implement So if we are already in production, we already have like multiple classes that are implementing this interface.

[00:05:58]
You can see how this poses quite a challenge. Once you've defined this interface, you don't even wanna update it or you're gonna break everyone who has implementing you. So that's why we have the default methods, they allow you to provide a default implementation, so that they are not abstract and your subclasses are not on the hook, for implementing those methods.

[00:06:32]
Now they can if they want to by just overriding the method, but they don't have to. So, this avoids breaking all of the implementing classes with these newly added methods. All right, so again, these methods are called default methods. They're methods within the interface that will allow the coder to provide a default implementation for this, and this ensures backward compatibility.

[00:07:05]
All right, so in order to make something default, you would simply write the word default in front of it. So I can say default getPrice and then I would need to provide an implementation. So, in here, this is really tricky, cause my initial thought is to make this price be zero, right?

[00:07:34]
Default initial, but what does that mean? Any class that has implemented me, has inherited getPrice. If they are unaware, that I even added this method then anyone who's using their class and they called getPrice, so it's free is free. So you set this just be careful, you can set it to whatever but some initial value so that we're not losing a ton of money.

[00:08:03]
Now if it's really that important that the all implementing classes update to provide implementation for this method, you just let stuff break. You make it a break and change, you don't put a default method. Stuff won't compile, and those authors will know, the interface was updated we have to take care of that.

[00:08:26]
>> Is there any way to make it return a message, let's get a really complex application. Someone's trying to figure out, well, okay, why is my code breaking? The interface updated, like director, return a message to the system that's using this interface, like to return an error message to them so they know what's happening.

[00:08:45]
>> No, you wouldn't do that, you just let it break. So when I go to open my stuff, I don't need a message from anybody because I can see it right here. And he's straight up telling me what's wrong and I say what? I haven't implemented all the methods, how not, what methods am I supposed to be implementing?

[00:09:06]
And then you see, crap, he added two new methods here. All right, so let's make this one default to setPrice ,I don't have a variable to even set this to. So I can leave this as just the curly braces alone, gives it a body. I can leave it just like that or I can not make a default and force everybody [LAUGH] to update their stuff Questions
>> It seems like default for getters and setters are less useful than other methods, is that it?

[00:09:53]
>> Yeah, yeah probably. Seems like with these interfaces, basically what you're fundamentally doing is forcing whoever's using your interface to make the promise that they're gonna fulfill whatever it is you're telling them it needs to have.
>> That's right, that's right, all right, let's talk about static methods a bit more.

[00:10:21]
Like default methods, static methods provide implementation, they differ in that static methods are not inherited, by the implementing classes and so, they cannot be overwritten. Okay. So this means that an interface's static methods are not available from the instance of an implementing class. So here, let's say I had a myStaticMethod and product, book would not be able to access that.

[00:11:01]
However, we could if we just said product., again, remember static methods do not require instantiation, which we wouldn't be able to do on an interface anyway. And so to access that, I would just say interface name dot and then access the method. All right, key points to remember about interfaces, they cannot be instantiated, just like abstract classes.

[00:11:28]
Interfaces can be implemented by classes. They can be extended, but only by other interfaces. Any class that implements an interface has to implement all of its methods, all of its abstract methods, or it must declare itself as abstract. And then by default, methods in an interface are public and abstract.

[00:11:56]
So you don't need that explicit declaration of such. Notice we didn't put public, we didn't put abstract. And then in addition to abstract methods, interfaces can also contain default and static methods as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now