Multiple inheritance
Hi all,
It is a known fact that java doesnot support multiple inheritance.How can this be achieved in java.Many people say that we can use interfaces and define methods inside them.But we cant write any code inside these methods.Then these methods will be used just for name sake i.e. similar operations are given common method name.What do to if I want multiple inheriatance.
I also know that we can declare classes inside interfaces.How can the objects of such a class instantiated?Is that the solution for the problem?
With regards
Satish
1.) Java does support multiple inheritance, just not of implementation, but of interfaces (as you've mentioned)
2.) Where do you need MI?
3.) Use composition instead MI.
4.) I've yet to see a good, usefull example of MI of implementation that couldn't be designed easily and more elegantly without MI.
I need MI when I have a method in a class and I want to use it one or more classes .
> I need MI when I have a method in a class and I want
> to use it one or more classes .
Why? Either it provides a functionality that should be encased in it's own class, or (if its a simply utility method) it should be a static method in a UtilityClass.
Can you give me an example? I doubt that MI would be the right way in this case.
Thank youTell me how to instantiate an object of class inside an interface.
An object of which class?
> Hi all,
> It is a known fact that java doesnot support
> multiple inheritance...
If your intention is just to shoot the breeze regarding Java or do a little research for a paper or exam then you're really better off running a search. This topic comes up at lease weekly.
If however you are faced with a real-world design problem then outline what you're trying to do and there are people here who can suggest your best options.
One of the common mistakes in OOP is to assume that inheritence is just a cool way to reuse code. Of course it is, but the primary driver is all about deciding what Classes of Objects can be expected to do. And that sounds a lot like what an Interface is for.
If you think you have a problem that can only be solved through MI, then you need to take a back seat and get some guidance from someone who can look at the problem from another angle.
> One of the common mistakes in OOP is to assume that
> inheritence is just a cool way to reuse code. Of
> course it is, but the primary driver is all about
> deciding what Classes of Objects can be expected to
> do. And that sounds a lot like what an Interface is
> for.
Exactly. and even in case of implementation inheriatance there are some rules in place which need to be followed. One being Liskov's Substitution Principle
> Liskov's Substitution PrincipleHm. Always bring Michael Owen on thirty-minutes from the end, never chance him for a full game? Sensible advice, I agree, but what's it got to do with programming?