Thursday, July 9, 2009

Why java will not support multiple inheritance like C++?

java supports multilevel inheritance but not multiple inheritance is there any specific reason explain me plz(if possible)?.

Why java will not support multiple inheritance like C++?
The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible without carrying over C++'s unnecessary complexity.





In the designers' opinion, multiple inheritance causes more problems and confusion than it solves.





However, you can use multiple interface inheritance through the use of interfaces. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.
Reply:With multiple inheritance, you can inherit the same class many times from different sources.





For instance, if we wanted to create a fuctionous James Bond like vehicle that is an automobile and drives like a car. Can change to a boat and travel on water, can change to a submarine and travel underwater, and it can also grow wings and fly in the air.





So this vehicle is a car, a boat, a submarine and a plane.





If we create an inheritance structure of


Parent: Vehicle


Child: Land Vehicle


Child: Water Craft


Child: Air Craft


Grand-Child: Car (parent: Land Vehicle)


Grand-Child: Boat (parent: Water Craft)


Grand-Child: Submarine (parent: Water Craft)


Grand-Child: Plane (parent: Air Craft)


Great-Grand-Child: Fictionious Vehicle (parent(s): Car, Boat, Submarine, Plane)





Now, if Vehicle contains any data, eg a weight or something, then who does Fictionous Vehicle inherits this property from?


Does it come from Vehicle? Land Vehicle? Water Craft? Air Craft? Car? Boat? Submarine? or Plane?





If Vehicle, or Water Craft contains a function, which actual function gets called when needed? The function in Vehicle? Water Craft? Boat? Submarine? Fictionous Vehicle?





This is just one of a number of reasons, and ways inexperienced programmers can get caught out.
Reply:Java does not support multiple inheritance. To some extent, the interface feature provides the desirable features of multiple inheritance to a Java program without some of the underlying problems.





While Java does not support multiple inheritance, single inheritance in Java is similar to C++, but the manner in which you implement inheritance differs significantly, especially with respect to the use of constructors in the inheritance chain.





In addition to the access specifiers applied to individual members of a class, C++ allows you to provide an additional access specifier when inheriting from a class. This latter concept is not supported by Java
Reply:java is not support multiple inheritance but there is a alternative way of doing multiple inheritance

survey monkey

No comments:

Post a Comment