Java's Creators Rejected Multiple Inheritance - Here's Why

  Рет қаралды 66,637

Coding with John

Coding with John

Күн бұрын

Java's creators didn't include multiple inheritance. Have you ever wondered why?
☕ Complete Java course: codingwithjohn.thinkific.com/...
Multiple inheritance was excluded from Java from the very beginning. But why was multiple inheritance not included in Java when so many other languages include it?
Learn all about Multiple Inheritance, including why Java doesn't have it, in this Java video tutorial.
Learn or improve your Java by watching it being coded live!
Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.
Let me know what else you'd like to see!
Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.
00:00 Why No Multiple Inheritance?
00:28 Inheritance in Java
02:40 Multiple Inheritance
04:05 The Diamond Problem
06:26 What Java Does About it and Why
08:14 Get What You Need Without Multiple Inheritance
📕 THE best book to learn Java, Effective Java by Joshua Bloch
amzn.to/36AfdUu
📕 One of my favorite programming books, Clean Code by Robert Martin
amzn.to/3GTPVhf
🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial
www.audibletrial.com/johnclean...
🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)
bit.ly/3QPNGko
📹Camera I use for recording:
amzn.to/3wlXcmR
🎙️Microphone I use (classy, I know):
amzn.to/3AYGdbz
Donate with PayPal (Thank you so much!)
www.paypal.com/donate/?hosted...
☕Complete Java course:
codingwithjohn.thinkific.com/...
codingwithjohn.com

Пікірлер: 274
@Riley-ml9vo
@Riley-ml9vo Жыл бұрын
I really appreciate the way you take high level concepts and present them in a digestible manner. I would love to hear you talk about design patters in Java. I feel it's a topic that is somewhat ambiguous and I feel that your high level analysis of it would greatly help me and many others!
@pawewodarczyk1546
@pawewodarczyk1546 Жыл бұрын
This problem somewhat returned the moment when interfaces got default implementations in Java 8. So in some cases a developer is forced to choose which interface default implementation he wants to use, but it's compiler enforced and quite rare in practice. As for a diamond issue the bigger problem in my opinion are fields rather than methods. In case of methods you can choose an implementation as mentioned before, but in case of fields you can't just "pick" which field version you want to use safely without potentially messing up something.
@mariovelez578
@mariovelez578 Жыл бұрын
You explain these concepts really well. Even after 5 years of coding Java, I still learn from you
@Brahvim
@Brahvim Жыл бұрын
I have the same amount of experience and I agree with you!
@SDFC
@SDFC Жыл бұрын
Spicy comment incoming: So, I can tell that this is a beginner friendly channel, but it would've been nice to at least mention in passing that inheritance is a bad practice ("Composition over Inheritance"). Here's a few resources that have great coverage of this concept: - I greatly appreciate that you've included "Effective Java" in the video description, which is a beautifully written book -- it actually covers this concept as Item 18, which is titled... "Composition over Inheritance". - I'd imagine that you're also familiar with the "Design Patterns" book by "the gang of four" (another very classic book in the industry) which also covers the concept of "Composition over Inheritance" in the very first chapter (p. 18 of my copy of the book). - Some languages such as Golang (the language that Google created for their own software engineers) go as far as to not even implement inheritance within the language. The alternative to solution to inheritance would be "delegation", which is composition + forwarding. This is a rather clunky pattern in vanilla approaches to Java, but I saw that you thankfully have a video on Lombok, which has the "@Delegate" syntax sugar as an experimental feature. I see you have some other videos that have already covered a lot of very awesome topics in a concise and beginner-friendly way, like Generics, Reflection ("meta-programming"), Lombok, Records (building block of algebraic data types), Checked vs Unchecked exceptions. The only unturned stone that I can actually think of is "Data vs Object", which Robert Martin covered in a great blog post over here: blog.cleancoder.com/uncle-bob/2019/06/16/ObjectsAndDataStructures.html
@domportera
@domportera Жыл бұрын
while I generally favor composition over inheritance, it's far from considered "bad practice" and is often the best way forward for extending pre-existing functionality. I feel like claiming that inheritance is bad practice is somewhat in-vogue. Granted it can (and often does) get messy, and so you *do* need to be careful with how you abstract things or else you definitely end up with a monster that can be difficult to tame. That being said, opportunities for Composition where it is not overly redundant should generally be preferred
@jozef_franek
@jozef_franek Жыл бұрын
maybe in this topic could have been mentioned implementation of method from two different Interfaces with same method signature
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I definitely thought about it! Basically how it works is, as long as the interfaces don't both have default implementations, there's no real conflict to resolve. The class can still implement that method that both interfaces specify. If they do both have default implementations, and you don't specify your own, it's simply a compilation error.
@DelzDerMeister
@DelzDerMeister Жыл бұрын
The same logic could be applied to multi inheritance. The only thing that would be problematic are non-private fields.
@nonswag4739
@nonswag4739 Жыл бұрын
Would be interesting to know why two similar methods but with different type parameters are not allowed in one class
@PieJee1
@PieJee1 Жыл бұрын
@@nonswag4739 in some strict languages that actually exists
@nonswag4739
@nonswag4739 Жыл бұрын
@@PieJee1 I know, that's why I'm wondering why java does not allow this
@sabei221b
@sabei221b 9 ай бұрын
I discovered your channel today, let me tell you, your content is incredible! I love how simplified yet informative your explanations are, it says alot about how well you know what you're doing, I love the jokes you implement here and there as well, the whole thing is very interesting and educational from start to finish, light hearted and well put together. perfect to my taste! 🎉
@Sollace
@Sollace Жыл бұрын
8:06 I've had a couple instances where I've wished Java had multiple inheritance. It wasn't really the only solution, just the alternative was a messy approach that involved duplicating a lot of code.
@Djaenzee
@Djaenzee Жыл бұрын
We had this multiple times in our code base, although the problem mostly came up when some of the actual java base classes better should have been designed to be interfaces. Seems to be especially true in Java UI classes that did not change mostly due to compatibiltiy reasons...
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
and then one day you realized, the best solution is just to not use java
@th3runaway
@th3runaway 8 ай бұрын
I'm curious if you had specific examples. I think in most cases, we've been conditioned to prefer composition instead of inheritance and I think in most cases, the projects I've worked on rely less and less on inheritance for behavior and really just the properties/values.
@Sollace
@Sollace 8 ай бұрын
@@th3runaway I think the use cases I had which this would would have helped with are both instances where I don't have control over what the parent classes are doing. From memory, (modding minecraft) one was adding functionality to both Villagers and Zombie Villagers. The other was using the same model for two entities but generic constraits prevent me from doing that, so I have to make a "Proxy" class for the second that wraps the model for the first.
@aminembh6094
@aminembh6094 Жыл бұрын
Great video as usual! You do a fantastic work, it’s appreciated, we need more people like you 🎉
@ManosSef
@ManosSef Жыл бұрын
I have actually tried to implement annotation interfaces in a class before and I was surprised when I saw you do it too. And yes I made a class that implements Deprecated, and realized that the required methods are actually the annotation members, as well as the annotationType() method. And while testing this, I found out that annotation interfaces without any members are actually functional interfaces, and giving them the @FunctionalInterface annotation works.
@rikschaaf
@rikschaaf Жыл бұрын
If the diamond problem is the reason for not having multiple inheritance, then why can you implement multiple interfaces that could have the same default method implementation? In my opinion, It would be neater if the subclass would treat a superclass method implementation conflict as an abstract method, so that you'd have to be explicit about which implementation you'd want (or if you wanted a whole different implementation). Only if the method was final it should give an exception.
@francoisemond3515
@francoisemond3515 Жыл бұрын
Thanks John, keep up the great work. I always look forward to watching your next video.
@lucashenrique6076
@lucashenrique6076 Жыл бұрын
I Really like the way you explain coding stuff, even things I already know I watch the video patiently and learn something new. The way you speak english is nice even for foreign like me. Thanks again John ! See you next time!
@bacon37460
@bacon37460 Жыл бұрын
Another awesome video. Thanks John for your efforts. Could you please make a video for us about dependency injection (DI and IoC)?
@hz4353
@hz4353 Жыл бұрын
John, just a great video, i would appreciate if you can have some more advanced course on Java such as concurrency, i would definitely pay to watch.
@lilygranger6264
@lilygranger6264 Жыл бұрын
Loved the video john! May you please making a video on interfaces?
@RenderDragon
@RenderDragon Жыл бұрын
Good video, especially at the end :)
@SamKhan-kb3kg
@SamKhan-kb3kg Жыл бұрын
Great elaboration, absolutely loved this video
@nitika9769
@nitika9769 Жыл бұрын
keep making these videos, get to learn so much from from you
@mastershonobi110
@mastershonobi110 Жыл бұрын
Great video. Thanks for the review. 🙌🏿🙏🏾
@shis10
@shis10 Жыл бұрын
Amazing Video. 🙌🏻 Please make full video on Interfaces, has relationship and is a relationship.
@Quinteger
@Quinteger Жыл бұрын
I find the diamond problem argument a bit of a meme because of the existence of default interface methods since Java 8. By that I mean it already exists in the language and you have tools to resolve that conflict while preserving the multi inheritance of interfaces. The more likely reason I can think of is performance - with single inheritance, class fields always have the same offset within the memory of an object and type casting becomes basically free.
@BlueRey2
@BlueRey2 Жыл бұрын
Hey John! Would be really cool if you made an explanation video for Futures, it seems like a feature like Optionals where it seems to be somewhat overlooked. Thanks!
@dalcod
@dalcod Жыл бұрын
Hi John, your teaching style is impressive. Do you, by any chance, have plans to create a course or series of videos on Spring Boot?
@purplestuffist
@purplestuffist Жыл бұрын
This video is amazing. Thanks for sharing this important information! Thanks John :)
@artworld9799
@artworld9799 Жыл бұрын
Very beautiful and interesting work!!
@henriquesenadev2194
@henriquesenadev2194 Жыл бұрын
Nice video bringing core concepts and a good mood!
@asyakatanani8181
@asyakatanani8181 Жыл бұрын
Well, John, very clear and to the point. I like it, thanks...
@zhalagurbanzade636
@zhalagurbanzade636 Жыл бұрын
The explanation is very clear and concise. James Gosling would be envious.
@Walkingdeadman1991
@Walkingdeadman1991 Жыл бұрын
I've got one!! Please believe me :P I'm working on a graphing package in Java, and one of the superclasses (abstract) is Plot, from which inherit LinePlot and ScatterPlot (not abstract). I have been wanting to create a LineScatterPlot class, and have kept on thinking that this would have been a perfect time for multiple inheritance. But yes, I will find a way around it... :P
@Daxbedivlogs
@Daxbedivlogs Жыл бұрын
Thanks for valuable content John... looking forward to Advanced java and framework topics as Spring, SpringBoot, completeable Future or Multithreading, Microservices etc @John
@ShadoFXPerino
@ShadoFXPerino Жыл бұрын
8:00 Well here's the sitch: Cat is an abstract class written by someone else that you aren't allowed to change. Dog is from a licensed library package which you don't have the source code for. Your manager is telling you to implement Cog in 5 minutes since "everything's been done already".
@thomaslong1576
@thomaslong1576 Жыл бұрын
I wish everyone could teach like you.
@tomok284
@tomok284 Жыл бұрын
Thanks for sharing. Could you make a video about the differences between abstract class and interface and when uses which?
@omerisk8934
@omerisk8934 Жыл бұрын
Hey, You are doing a great job thank you for much for all efforts. When you explain a concept it is easy to get the logic but to make it more clear for us would you mind doing some implementation videos from basic to advanced ? Thanks in advance..
@Tech_Publica
@Tech_Publica Жыл бұрын
Overall excellent video, citing Gosling and stressing the "philosophy of simplicity" of Java is spot on, but I think one should make the distinction that using interfaces only gives you the same advantages as multiple inheritance as far as polymorphism is concerned, but NOT as far as code reuse is concerned, and inheritance is at its core a code reuse technique. But then again, there are other ways to achieve code reuse, like composition, so refusing multiple inheritance makes sense if you are after a language that maximise simplicity and not flexibility and power.
@gohardovlatyan6003
@gohardovlatyan6003 Жыл бұрын
This is brilliant, thank you so much
@MrAikien
@MrAikien Жыл бұрын
Great video! Thanks! 👌👍
@kchemutai3483
@kchemutai3483 8 ай бұрын
I actually enjoyed it. Thank you
@ChrisBrown-dn3tf
@ChrisBrown-dn3tf Жыл бұрын
I was wanting for multiple inheritance recently. I had a PythonScript class that acted as a large superclass for anything that was a script (entity script , item script). I also had a class called UserInterface which was a large superclass for classes that are UI (editorUI elements, Main menu UIs). I needed a UIScript class which would extend both and have the needed protected fields from both but it wasn't possible under the current system. UIScript extends PythonScript and within the script file, an object called ScriptedUI is constructed and used as the actual UI. Other than this recent problem, I would never have needed MI.
@moomoomoo309
@moomoomoo309 Жыл бұрын
Default implementations on interfaces are similar to MI for Java, so you could have used that, probably.
@gabrieljonsson7821
@gabrieljonsson7821 Жыл бұрын
Seems to me what Java actually need is the friend keyword to access protected fields of another class (without the fuss of package private arrangements).
@saipavan5032
@saipavan5032 Жыл бұрын
Great Video, It would be great if you can make a video on Interfaces as well.
@nandhineeneelakandan8156
@nandhineeneelakandan8156 Жыл бұрын
Love your videos. Can you do one executor service?
@jaylawlmc1915
@jaylawlmc1915 Жыл бұрын
Hello John! I am recommending "Future" and "CompletableFuture" as topic for one of your next videos. Would love to see your take on it. :-) Cheers
@satricon
@satricon 6 ай бұрын
Hahaha, this was awesome! I chucked at the "cog" fetch of "the cat does it if it feels like it" 😆
@Speiger
@Speiger Жыл бұрын
Actually the "Diamond Problem" can happen with Java8 thanks to default implementations in interfaces. Where functions are 100% equal and thanks to 2 different interfaces you have this exact problem. How does java solve it: The class has to actually decide which implementation it uses. So the user gets told please pick for us!
@kikoalbiol
@kikoalbiol Жыл бұрын
At the end Java arrived to same solutions that C++ and other older languages arrived 20 years before
@bluecup25
@bluecup25 Жыл бұрын
​@@kikoalbiol Yeah, same for C#.
@ericpenrose3649
@ericpenrose3649 Жыл бұрын
Python developer here: Its always baffled me that people actually use multiple inheritance. Not only have I seen it in in multiple stack overflow answers and tutorials, but actually in production. Even if you try to avoid the diamond problem by inheriting from very dissimilar classes, for instance a Pet class inheriting from Animal and say Property or something, and you make sure to avoid generic and common attribute identifiers like 'name', there are still a ton methods included to every class by default which can conflict, at least in Python. Maybe there are instances where they are fine in theory but my experience with them in production is ALWAYS frustrating. To any future or current developer out there: If you find yourself typing multiple-paragraph comments describing the quirks of something, maybe instead spend that time finding a better way which doesn't require it?
@stephenJpollei
@stephenJpollei Жыл бұрын
Python is hugely dynamic and has a bunch of duck-typing. Heck, I think that a person could have an fully constructed object that is an Animal and you could inject Pet into it, at run-type.
@DrSpooglemon
@DrSpooglemon Жыл бұрын
Multiple inheritance is generally used to combine a stateful class with a behavior class. Like having 'button' behavior which can be attached to an image or a label. Rather that for trying to do something crazy like combine an image and a label.
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
It's always baffled me that people use inheritance at all with non-abstract parents. In my experience inheritance rarely makes sense, and when it does, it only requires one abstract base class with multiple children. I've never needed grandchild classes or these stupidly complex inheritance hierarchies that people make UML diagrams for. That's how you overengineer your code and turn a simple problem into a complex problem and a bunch of "what ifs"... "oh well if Requirement A ever changes then we already have this abstract BeanManagerFactory class and implementing it would be trivial..." "if product ever needs this feature we could implement it easily, all we need to do is create a class for it and override this method from 5 parents up..." if you don't need it then don't build it ffs
@johngeverett
@johngeverett Жыл бұрын
Issues like this exist in any language, and are best resolved by discipline, or 'standards'. The better resolution IMO would be to force the descendant class to implement the duplicate method as though it were a 'virtual method'. The 'interface' just makes you code ALL the methods for EVERY class that needs it. One way I get around the limit of only one parent class is to have a class that has an instance of each additional class that I might want. No ambiguity of methods, since the same-named methods are called by explicit reference to the enclosed object(s). BTW, the resultant creature would be a 'chimera'.
@lorenzmuller5083
@lorenzmuller5083 Жыл бұрын
Awesome video! Question: is the @override annotation necessary or could it be omitted when implementing the interface methods? If so, when do we need it?
@JorgetePanete
@JorgetePanete Жыл бұрын
Override doesn't compile if there is not method to override (for example, when its name changed), if you had no override it wouldn't warn you
@dgront
@dgront Жыл бұрын
It has been clear for me that multiple inheritance is not necessary since I started programming in rust. There is no inheritance at all in that language. There are interfaces in rust however.
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
rust did it well. inheritance is unnecessary and encourages you to over-engineer your code. traits in rust are fantastic because you get exactly as much abstraction as you need without going overboard
@malakabuhejleh358
@malakabuhejleh358 Жыл бұрын
It’s very helpful thank you
@joeysarie1520
@joeysarie1520 Жыл бұрын
There one case where the multiple inheritance is missing, it's when you want to inherit from a class that it's not yours and don't duplicate code. For exemple, if you have an interface that allow to get/set data from a database with a standardized solution (use of getType() to know table, then fetch provided attribut) and give those function to 2 objects, then you can either duplicate the whole code, or create a common abstract object. But what if you want to add this feature to object that can be create in database, like MyCat & MyDog, but you can't on the Animal, Cat & Dog because it's not your ? Before Java 1.7, it was not possible without code duplication. Since Java 1.8, you may try to provide default function on interface, but you still need a getType() which is not available in your interface. You can trick java by creating this method in the interface, because if the method exist in at least one of your parent, compilation work. The downside is that you need to declare an AnimaleLike interface to provide signature of all needed function to your own interface, so you still have a bit of a duplication.
@jbragg33
@jbragg33 Жыл бұрын
But there is one thing : Mixins. Java not having multiple inheritance, means it does not have mixins ! My use case was this one : I was using SPRING HATEOAS, and this framework _forces_ you to have "Resource classes" that extend some class : class MyResource extends HateoasResource. Our domain objects all had some common attributes (creationDate, lastModifiedDate, creator, updator, etc), and the *most elegant solution* would have been mixins.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I'll have to check more into that! I'm familiar with hateoas, but haven't used it in any real-world app as of yet. Looking at the documentation though (haven't tried this yet), can you use EntityModel as a kind of wrapper, and have be your domain class extending whatever you need?
@Sheikhhamiz
@Sheikhhamiz Жыл бұрын
@@baranosiu as far as I know multiple inheritance and mixins are two different things, mixins can be used to copy methods to prototype
@jbragg33
@jbragg33 Жыл бұрын
@@CodingWithJohn Hm not sure, we were using "RepresentationModel", the way we did it was creating another Superclass containing the common fields, that works of course, but it needed a bit of refactoring. In the context of "extending existing code", that does not feel good to change a hierarchy, thus mixins were missed by me this time just for elegance. But I agree you can do without them :)
@andreassumerauer5028
@andreassumerauer5028 Жыл бұрын
To me multiple inheritance is just another useful tool in the box that unfortunately is not available in Java. One appropriate use would be the implementation of modular classes that can then be combined into a whole range of different combinations. Coming from C++ I sometimes do miss that language feature. Of course one can emulate that behavior with Java constructs like helper classes and/or default implementations in interfaces (which again introduces the oh-so-terrible diamond problem that doesn't actually exist in Java.). At the end these workaraounds require me to spread code all over the place that IMO better should be kept together in a single class. Of course one can do all kinds of stupid things using multiple inheritance. As with almost any other advanced tool. Carpenters use nail guns. When used appropriately they increase productivity and allow to work faster and with more accuracy. Sure you can shoot yourself in the foot with a nail gun. That is why you never let the inexperienced apprentice use it unsupervised, you observe best practises and safety procedures and you always wear protective gear. Only the Java language designers would come to the conclusion that nail guns should be banned altogether.
@the00one45
@the00one45 Жыл бұрын
Hello John, nice video! Quick off topic question: I've seen that you have switched from Eclipse to IntelliJ in your videos. Is there a specific reason why? I've heard a lot of people praise it, but besides some preferences never seen the great benefit.
@Rnhardt542
@Rnhardt542 Жыл бұрын
I can tell you my experience, I was obligated to use Eclipse on one of my projects, HATED IT. From navigation to ease of using plugins and most especially, code suggestions. No matter what I could think of, any action I needed to take in Eclipse I could cut it in half in Intellij, at least this was my experience.
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
because eclipse is shit. try installing a plugin, any plugin. you can't! eclipse just can't do it without throwing some fatal error. and how about that UI? it has no right to be so complicated. good luck importing an existing project into eclipse without fucking it up somehow. and good luck creating a git repo that doesn't have an extra top-level folder...
@rubenlarochelle1881
@rubenlarochelle1881 Жыл бұрын
An alternative could be to allow multiple inheritance for the cases without conflicts and only give out an error when a diamond happens...
@gonegahgah
@gonegahgah Жыл бұрын
The other important hiccup with the diamond problem is that if both classes (ie dog and cat) need to both adjust something when a method call occurs, but only one is called, then part of the functionality that should occur does not. Either the dog or the cat fails to do its tandem essential action. There is also the problem where, if dual action is resolved and done, that the cat and dog may try to do the same action in animal when that action is only required once. If they for example both incremented a counter in animal it could be problematic...
@javalady
@javalady 21 күн бұрын
"I dont beleive you" was really good :))
@deconline1320
@deconline1320 Жыл бұрын
And then they added the Default methods in interfaces in Java 8... which introduced multiple inheritance. Many of Gosling initial design concepts are now gone from modern Java. But that's what usually happens to successful programming languages.
@darkferiousity
@darkferiousity Жыл бұрын
This cog class got my head grinding gears.
@christopherbaviera197
@christopherbaviera197 Жыл бұрын
For instance, since makeNoise() is a behavior, could be implemented as an interface describing better animals possibilities
@ripanpramanick425
@ripanpramanick425 Жыл бұрын
It would be really helpful if you could bring some Design pattern videos
@santi9775
@santi9775 Жыл бұрын
Amazing explanation! I have just one suggestion for future videos: I tend to see your videos at night, everything good when you’re writing code, but when you use the board it hurt my eyes! Haha Just a silly suggestion, really appreciate your videos! :)
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I'll try and use a dark background on the board next time!
@santi9775
@santi9775 Жыл бұрын
@@CodingWithJohn Thanks! :)
@dashknow5082
@dashknow5082 Жыл бұрын
Topic Suggestion : Shallow Copy vs Deep Copy of List or any Collections in Java
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I like it, it's on the list!
@mouradvip7
@mouradvip7 Жыл бұрын
Hello John, Great content as always, you have a natural gift of explaining complex concepts, and I bet you can explain Design Patterns to a toddler. it will be delightful if you can upload a video on Java Streams API. Much Love.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I do have a toddler son, so I'll have to give that a try!
@TBiscuit1
@TBiscuit1 Жыл бұрын
I feel like Java should have done something simillar to what Kotlin does with classes that have grand-parents (Fun fact, Kotlin kind of implement multi-inheritance, tho it is quite complicated and I never touched it) When a class is 3 layer of more, you can do super.function() to choose which version of the function you want So if we have ClassA, ClassB that is a subclass of ClassA and ClassC that is a subclass of ClassB, if they all implement the function "foo()", with ClassC, when you do "foo()" it will do ClassC foo(), if you do super.foo() (or super.foo()) it will do ClassB foo() instead and finally, if you do super.foo(), it will do ClassA foo() I feel like Java should have implemented something like that (detect when an ambigious function is called and cause an error if it cannot choose) Tho I fully agree that Multi-inheritance is extremely rarely useful
@samluther2649
@samluther2649 Жыл бұрын
I could be incorrect, but I feel this argument only holds water as long as you only accept the concept of an is-a relationship and reject the idea of a has-a relationship. Whether you like using has-a relationships or not they are a CS concept that would work better with multiple inheritance. Also in addressing the statement "I have never seen a problem that couldn't be addressed without multiple inheritance" I would say that I doubt there exists a problem that absolutely needs inheritance period. I'm quite certain that, while redundancy is bad, it still compiles. I might also say that this extends over multiple inheritance as well. While I could obviously not come up with a problem that needed to be solved with multiple inheritance (duh) I could come up with an example where it may have slightly less redundant code and could be slightly more readable. Especially in the realm of several has-a relationships.
@richskater
@richskater Жыл бұрын
I was certain that if you didn't specify the access level of a field it would default to protected. Blew my mind that you had access to .name in the main method; I had to go try it for myself. Learned multiple things on this one.
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
It defaults to "package-private," meaning that the field is only visible to classes in the same package. There is no "package-private" keyword so the only way to make a field package-private is to leave the access modifier off. Doesn't make any damn sense for a language that's trying to be simple and explicit
@matiturock
@matiturock Жыл бұрын
Could you make a video explaining the principles SOLID, DRY, KISS, and more?
@raphaelmateusdasneves772
@raphaelmateusdasneves772 Жыл бұрын
When i stumbled across this "missing" feature it was due to game development. It is really useful when you have stuff like a position class with move methods, a rotation class with rotation methods etc and want to apply it for example to a player class. But usually if you run into that you already fucked up your project architecture so its fine i guess. A better solution would have been to force the programmer to pick a method, for example with @Override(Dog.class) protected void makeNoise(); Then the override annotation would actually be useful nowdays lol
@schwingedeshaehers
@schwingedeshaehers Жыл бұрын
There was also the idea, that you could be forced to implement the duplicate methods yourself, and the rest is standard behaviour. I think overriding one of more methods with the same name is no good way. Does java has super? (I think so) Than it is important, that you can say which one you want
@schwingedeshaehers
@schwingedeshaehers Жыл бұрын
And i think there can be incomplete classes (I think it must also be possible for interfaces) -> you have to similar functions with different return types, but everything else the same. > How do you know which one is needed
@raphaelmateusdasneves772
@raphaelmateusdasneves772 Жыл бұрын
@@schwingedeshaehers My solution to this would be, to either use MyClass.myMethod(); for ambiguous calls or even allow blocks with a syntax like "using MyClass as super { ... }"
@schwingedeshaehers
@schwingedeshaehers Жыл бұрын
@@raphaelmateusdasneves772 we have a function in one class that returns an Integer, and one, that returns a string. -> how should that be handled?
@raphaelmateusdasneves772
@raphaelmateusdasneves772 Жыл бұрын
@@schwingedeshaehers I cannot happen. Unless you have different parameters, which is casual overloading then, you are forced to decide which class to implement. And having two methods with the same signature in one class is impossible anyways.
@fantasy9960
@fantasy9960 Жыл бұрын
Thank you!!!
@S4Fumbi
@S4Fumbi 11 ай бұрын
i don´t really get interfaces, if u are going to write everything from 0 why don´t i just write fetch in the cat klass without having to implement the interface? at the end i would like to have 10 different klasses with method x, i will have to write method x on 10 different klasses anyway. Is it just to make SURE that i implement it? or does it have more purpose?
@ceceliadixon765
@ceceliadixon765 Жыл бұрын
Hey John, would you please do a video on double dispatch :)
@AllanAquinoVieira
@AllanAquinoVieira Жыл бұрын
Is it possible to declare an optional methods in an interface, or all methods declared in an interface must be implemented?
@programmingwithnit5308
@programmingwithnit5308 Жыл бұрын
Well. thank you so much🙏🙏. I like your video
@AshishLohia70
@AshishLohia70 Жыл бұрын
nice explanation.
@MusabYK
@MusabYK 3 ай бұрын
Can't thank you enough 🙏🏼🙏🏼🙏🏼
@charleswelty2735
@charleswelty2735 Жыл бұрын
I couldn't find where to buy your Java text. Shop takes me to Area 120. Is that where the store is?
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I should remove that shop option, it's just not working right. There should be a link in the description.
@nonswag4739
@nonswag4739 Жыл бұрын
Can someone recommend a good source for learning about jni or (preferred) panama?
@ruster5517
@ruster5517 Жыл бұрын
Awsome video as usual
@motivationalthinking71
@motivationalthinking71 Жыл бұрын
Hey John, could you explain which IDE is better for Java, Eclipse or IntelliJ IDEA? I was a follower for a couple of months and just realized you switched your ide in the last year.
@CodingWithJohn
@CodingWithJohn Жыл бұрын
I generally prefer IntelliJ these days but I used Eclipse for years and it's great too. I started the channel with Eclipse but ended up doing a poll of viewers and a clear majority were using IntelliJ. So I thought it would be better to use IntelliJ than Eclipse in my videos for that reason.
@XIIOSRS
@XIIOSRS Жыл бұрын
Very informative, thanks!
@aamirshekh934
@aamirshekh934 Жыл бұрын
we want more conceptual java video like: Streams
@brunogreco290
@brunogreco290 Жыл бұрын
Hello, please a vídeo of Functional Interfaces. Predicate, Consumer, Supplier
@anketpatel2312
@anketpatel2312 Жыл бұрын
Hello . . I think 2 interface with same default method can have the same issue like dimand problem . But we must have to implement it and have to define which one we want to call. Like super.Interface.default();
@CodingWithJohn
@CodingWithJohn Жыл бұрын
When you implement two interfaces with the same default method, it's a compilation error that you can only resolve by implementing that method yourself
@kikoalbiol
@kikoalbiol Жыл бұрын
Multiple inheritance is not designed to provide a dog+cat child diamond problem, in fact you can have compiler problems doing so. Multiple inheritance is to adress what was latter provided in Java by interfaces. So multiple inheritance is required in many many problems to provide elegant solutions about what is in an object. The reason is that you can inheritate from an implemented class and this is not posible with interfaces where you must rewrite everything.
@4srinivas
@4srinivas 9 ай бұрын
That must have been an intense discussion
@redcrafterlppa303
@redcrafterlppa303 Жыл бұрын
Can you please make a video about the java9 module system.
@bauckrob
@bauckrob Жыл бұрын
There was a time, long ago, where people thought that extending classes and some of their methods was a great idea. By extending methods, one had a situation where the method in the child would call its base/super class' method, and add some extra behaviour. But which parent should we be calling? In C++, the programmer have to choose. In Python, every class has a "method resolution order" where in this case, a Cog would have Cog -> Cat -> Dog -> Animal. In general, people also thought that mixin classes, e.g. classes that would slightly change some behaviour by overriding methods, was a good idea. It seems like these ideas are mostly gone, however. It might work in the examples, but not in the real world, and modern design tends to have replaced the need for mixins and similar with smaller policy classes/functions. One can ask: If java was designed Today, would it have inheritance at all?
@harleyspeedthrust4013
@harleyspeedthrust4013 Жыл бұрын
if java was designed today it would surely be the same outdated pile of long words that it is right now. generics would be done just as badly, everything would need to be in a damn class, the standard library would still be littered with long names, and function types would still be clunky and ugly. inheritance is overrated and is a good solution to a niche set of problems. most of the time you don't need inheritance and trying to use it slows you down. java encourages you to overengineer your code by forcing you to use inheritance when you probably want something better
@lovetyagi5266
@lovetyagi5266 Жыл бұрын
please make video on java multithreding and cuncurrency
@achetadomesticus
@achetadomesticus Жыл бұрын
Thats the reason for me to Extends the Suscribe class and implement the Like interface ❤ Derpalerp!!
@aryanmaftooh7496
@aryanmaftooh7496 2 ай бұрын
That was lovelyyy❤
@cyberkiller83
@cyberkiller83 Жыл бұрын
Considering that java now supports implementations on interfaces and you can implements from multiple interfaces... so if you have one methjod implementation by each interface, could you be running ons ome kind of diamon problem here?
@chethans7436
@chethans7436 Жыл бұрын
You can't implement a method in an interface. Hope this helps 🙌
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Right, you can have default implementations in interfaces now. To answer your question, if you implement two interfaces that have the same method, both with default implementations, it just won't compile. You are then forced to either add your own implementation or remove one of the interfaces. Give it a try and check it out!
@chethans7436
@chethans7436 Жыл бұрын
@@cyberkiller83 Yes, that's right.
@cyberkiller83
@cyberkiller83 Жыл бұрын
@@CodingWithJohn Thank you John. I love your videos, i feel they make me a better developer.
@DerDoMeN
@DerDoMeN Жыл бұрын
@@CodingWithJohn That's definition of multiple inheritance while banning diamond inheritance... But some people have to bend over backwards to pretend it's not. I now remember a few (~7 or maybe even more) years ago when I saw that Java got this feature - I was at work and started laughing and then saying to my co workers "is Java trying to become a useful language :) Thanks for reminding me :) So to be honest maybe the initial creator didn't like multiple inheritance but Oracle/later maintainers after the initial author left, certainly disagree with him :)
@gabrieljonsson7821
@gabrieljonsson7821 Жыл бұрын
I don't think Java needs multiple inheritance, but I do think to be more flexible it should at least support friend classes. Many problems that at first glance would need it could in fact be solved with a combination a member of the other class combined with the friend operator of C++. So why is there no friend operator in Java? And no I don't think package private concept cuts it.
@dhilip77
@dhilip77 Жыл бұрын
Can you explain list
@luischavarria6104
@luischavarria6104 Жыл бұрын
John is there any book I may read for good practices at programming?
@CodingWithJohn
@CodingWithJohn Жыл бұрын
My favorite is Clean Code. I have some links for it in the description!
@-karter-4556
@-karter-4556 Жыл бұрын
can you do a video on sockets?
@alejandrobravo1221
@alejandrobravo1221 Жыл бұрын
Hi teacher, what theme are you using?
@CodingWithJohn
@CodingWithJohn Жыл бұрын
Just the IntelliJ default dark theme, with the background manually darkened a little more
@Axel_Andersen
@Axel_Andersen Жыл бұрын
Nice video: couple of comments. Someone somewhere mentioned that Erlang handles multiple inheritance particularly nicely. Being a Java programmer who doesn't want to expand to esoteric languages I have never bothered to find out if this is true or not. I find it odd/wrong that you draw the arrow in the class hierarchy from parent to child. It makes little sense that no parent know about its children and in reality no class knows about the classes that extend it. Not a big deal, but I had to get this off my chest. About interfaces, yes, they are useful and have their place. However with the kind of software that I develop I often want to know what happens when I call a particular method (most of the code is internal to the project so I can and need to check that). If the object type is an interface it is a nuisance cause the IDE does not know the implementing class and thus cannot take me to the code that actually gets called.
@johannes7110
@johannes7110 Жыл бұрын
I'm learning java and started with intellij but then I switched to eclipse and do not look back. I like your older videos where I could follow along better since you use eclipse. Why did you switch to intellij, do you think it is better?
@ChrisAthanas
@ChrisAthanas 3 ай бұрын
Eclipse is deprecated and JetBrains is multiplatform Do the math!
@JohannesJurieCoetzer
@JohannesJurieCoetzer Жыл бұрын
Looks like good classes, only bad thing is that it is not organized into a followable course.
@wolfrikz7238
@wolfrikz7238 Жыл бұрын
Please do a tut on 'Serialization'
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 75 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1 МЛН
Can You Draw The PERFECT Circle?
00:57
Stokes Twins
Рет қаралды 90 МЛН
КАК СПРЯТАТЬ КОНФЕТЫ
00:59
123 GO! Shorts Russian
Рет қаралды 2,6 МЛН
FOOTBALL WITH PLAY BUTTONS ▶️ #roadto100m
00:29
Celine Dept
Рет қаралды 72 МЛН
Кәріс тіріма өзі ?  | Synyptas 3 | 8 серия
24:47
kak budto
Рет қаралды 1,7 МЛН
LeetCode Exercise in Java Tutorial - Two Sum FAST Solution
23:36
Coding with John
Рет қаралды 162 М.
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 881 М.
Kademlia, Explained
24:22
number 0
Рет қаралды 14 М.
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 600 М.
Fundamental Concepts of Object Oriented Programming
9:16
Computer Science
Рет қаралды 837 М.
Set and HashSet in Java - Full Tutorial
20:43
Coding with John
Рет қаралды 190 М.
Git MERGE vs REBASE
16:12
Academind
Рет қаралды 1 МЛН
Can You Draw The PERFECT Circle?
00:57
Stokes Twins
Рет қаралды 90 МЛН