answersLogoWhite

0


Best Answer

Java does not support the ability for one class to extend more than one other class. However, Java does allow for one class to implement any number of interfaces.

Let us consider the below example for hybrid inheritance.

public class A extends C implements D, E {

}

Here A extends C which is direct single inheritance, By implementing D & E we are trying to achieve multiple inheritance to the limit that Java allows us. And if C in turn has extended some other class then we have multi level inheritance as well.

This can be termed as Hybrid inheritance. The presence of more than one form of inheritance.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

"Hybrid" inheritance is a compromise between strictly single inheritance and true multiple inheritance.

Single inheritance means that one class may have exactly one parent class. While this is nice to have, it makes creating complex systems an even more complex process.

Multiple inheritance means that one class may have any number of parent classes. This is one of the features of C++ that the designers of Java wanted to do away with. True multiple inheritance can introduce ambiguity when two different parent classes each has a function defined with the same name and number/type of arguments. The program has no idea of knowing which one is supposed to be called, and how the choice is made is often a non deterministic, implementation-specific rule.

The designers of Java wanted something more flexible than single inheritance, but not as complicated and problematic as multiple inheritance. So they came up with what is known as hybrid inheritance. This is the system which differentiates between a class and an interface.

A class in Java inherits from one other class (if no class is specified, it inherits from class "Object"). That same class may also inherit from any number of interfaces. The reason for this is to remove any ambiguity for duplicate method definitions. Since each class may only inherit from one other class, it is syntactically impossible to have multiple definitions for the same method.

Let's look at an example...

// This class has one method in it, which adds an Object to an ArrayList.

class A {

public ArrayList list;

public void add(Object o) {list.add(o);}

}

// This interface defines a single method: add

interface B {

public void add(Object o);

}

// This interface is identical to interface B

interface C {

public void add(Object o);

}

class D extends A implements B, C {

}

This "empty" class avoids any kind of method definition collision, even though each of the three classes and interfaces it inherits from define the same method. It can do this because only one implementation of a method can ever be passed down to a child class.

Since method implementations are not allowed in interfaces, you are guaranteed to encounter one of two scenarios:

  1. The interface method hasn't been implemented and your class needs to do so.
  2. The interface method has already been implemented in your class's superclass and so your class does not need to do so (though you can override it if you want to).

Note that there is no combination of classes or interfaces which will present a case of having multiple implementations of the same method.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

4. Hybrid Inheritance

This is the scenario wherein your java code exhibits traits of more than one of the inheritance types:

Public class FerrariF12011 extends Ferrari implements Car, Automobile {…}

The above is a combination of both single and multiple inheritance.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In Java what is hybrid inheritance?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What kind of inheritance is not allowed in java?

Java does not allow the multiple inheritance of concrete classes, though it does allow a "hybrid" inheritance of one concrete class and multiple interfaces.


What types of inheritance in Java programming?

Java uses a hybrid system of inheritance. The designers chose a compromise between strict single inheritance and full multiple inheritance.See the related questions section below for more information.


Does hybrid inheritance consist of ANY two types of inheritance?

There are only two types of inheritance to begin with: single inheritance and multiple inheritance. Since they are mutually exclusive there is no such thing as hybrid inheritance.


What does java not support?

Java does not support multiple inheritance.......


What is ambiguity in multiple inheritance?

Java does not support multiple inheritance


What is problem in multiple inheritance?

Java does not support direct multiple Inheritance. Harder to implement, not every language support it: C++ does, Java does not.


Give the structure of multiple inheritance?

Java does not support multiple inheritance. It is done with the help of interfaces in java. a class can implement n number of interfaces, thus showing multiple inheritance. but a class cannot extend multiple classes in java.


What is public inheritance in java?

Inheritance is a Java feature by which we can reuse code and programming logic from one class in another class. We implement Inheritance using the extends keyword.Ex: public class Ferrari extends Car {…}Here the Ferrari Class will extend features from the Car Class.This is Inheritance. The different types of Inheritance are:Single InheritanceMulti-Level InheritanceMultiple Inheritance (Java supports only Partial Multiple Inheritance) andHybrid Inheritance


How ploymorphism and inheritance is different from that in Java and c plus plus?

C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick. For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.


Need of interface in java?

Interfaces are used in Java to accomplish most of the goals of Multiple Inheritance. For several reasons, Java only supports Single Inheritance for classes - i.e. a class can have only a single parent. The use of Interfaces is how Java attempts to implement most of the positives of the concept of Multiple Inheritance while avoiding its pitfalls.


Why you neet interface in java?

When you need the benefits of multiple inheritance while avoiding the DDD (Deadly Diamond of Death). Java doesn't allow multiple inheritance anyway.


How do you implimenting multilevel inheritance in java?

In java we can implement more than one interfaces for a single class but we can't extend a class to more than one super class so ,java indirectly supports multiple inheritance.