www.fgks.org   »   [go: up one dir, main page]

answersLogoWhite

0


Best Answer

The multiple inheritance is supported by using more implements key word

example

class a implements b, implements c, implements d

{

....

}

so here the class a supports multiple inheritance by implementing class b,c,d.

Java does not support true multiple inheritance - true M.I. requires that a class be able to inherit directly from multiple classes. That is, in M.I., Class A can be a direct child of Class B and C.

To get much of the functionality and advantages of the M.I. model, Java uses Interfaces. Thus, a Java Class A will be the child of Class B, but can implement several interfaces X, Y, and Z. This attempts to mimic the behavior that in a M.I. language would be given by Class A being a direct child of B, X, Y, and Z.

The major difference between a M.I. language and a single-inheritance language with interfaces (like Java), is that interfaces can only define the method signatures and constants; they CANNOT provide implementations of those methods in the interface specification. Thus, any class which implements a given interface must provide an actual implementation of the methods that the interface describes. The big downside to this is that it can lead to a large amount of "code copying" - that is, if both Class A and Class B implement an interface X, there is a good chance that most of the methods from X will have the same code implementation in both Class A and B. Sometimes this is unavoidable; however, it is also considered good programming style to use abstract Classes to implement commonly used interfaces, then put that abstract class fairly high in the object hierarchy, allowing large numbers of classes to inherit from that abstract Class (which, in turn, means they have access to the implementation of those interfaces without having to copy the code). The advantage of interfaces is that they can keep things conceptually clean, and also make it simple for classes to decide how they want to implement the method contract the interface describes.

Practically speaking, if you would like Class A to inherit from Class B and also to inherit from Class C, then, in Java, you would simulate this via interfaces this way:

public class B {

// the superclass

methods_go_here_with_their_implementations;

}

public interface C {

// the Java equivalent of Class C in a M.I. Language

the_method_signatures_of_C_go_here;

}

public class A extends B implements C {

implementations_of_methods_in_C_go_here;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement multiple inheritance through interface in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How to write a code for a program that uses inheritance?

Multiple Interfaces are usually associated with name ambiguity and this is the reason that they haven't been implemented in .NET. However in order to incorporate the advantages of multiple inheritance keeping aside the issues involved, .NET has come up with one more concept known as INTERFACE. Interfaces allow a class to inheirt behavioral characteristics frm more than one object type called interfaces. Classes implement the interfaces. You can check out the associated links for a number of examples demonstrating above concept.


Why is multiple inheritance implemened through interfaces?

Interfaces have only methods declared and not defined. So that these various methods can be altered or customized or written according to the need. Hence Multiple inheritance is implemented through Interfaces.Interface is a progam which just declares various methods or functions, so that any user can write the contents of that method declaration.AnswerTrue Multiple Inheritance refers to the ability to inherit from multiple CLASSES. Thus, a true multiple inheritance language does not use interfaces. However, many single-inheritance languages provide most of the benefits of the multiple inheritance concept by using interfaces (Java, for example). The problem with true Multiple Inheritance is twofold:(a) at the compiler and runtime level, it is often difficult to determine which method is being referred to, as M.I. can lead to very convoluted (and sometimes conflicting) namespaces(b) there is no specific superclass, which can lead to problems with determining relationships between classesUsing a S.I. model with interfaces avoids these two problems.


What is example of character user interface is?

A memory in which information can be stored and erased multiple times is?


What is an interface?

An interface in programming is a contract that defines a set of methods that a class must implement. It allows for standardization and abstraction in code by providing a clear way for different classes to interact with each other. Interfaces do not contain implementations of the methods; rather, they specify what methods a class must have.


Who can tell me what is inheritance tax?

Inheritance tax is the tax that you have to pay if you gain some kind of income through an inheritance fund. It is like adding to the income you gain through inheritance.


Which type of inheritance does not support by java?

Desoola Anil Kumar, Java Faculty: There are 5 types of Inheritance. 1.Single inheritance, 2.Multiple inheritance, 3.Multilevel Inheritance, 4. Hirarichal inheritance and 5. Hybrid inheritance. Java supports multi level inheritance and hirarichal inheritance.


What is inheritance in science terms?

Inheritance is the process by the which traits or characteristics pass from parents to their offspring through genes. Some of these characteristics are dominant and some are recessive and they are passed through inheritance.


What is an interface in C?

C doesn't have interfaces like Java does. In fact, it doesn't even have classes. The closes thing it has to interfaces might be a pointer to a function, which could point to one of several different functions without the caller having to know which one exactly. C does not have interfaces or classes. However it's quite easy to simulate interfaces and classes in C. Before I get into doing that, let me explain what interfaces are and how it's done in C++, which supports multiple inheritance which is much more powerful then interface inheritance. Interface inheritance is basically the same thing as multiple inheritance. In interface inheritance, the interfaces are classes that cannot have method implementations and variables. They may only have function prototypes. You are only allowed to inherit from one full blown class as a child in the interface inheritance model. However, in multiple inheritance (which C++ supports), you're allowed to inherit from as many full classes as you want. This allows you to make those full blown classes as slim as a bunch of pure virtual functions (interface) or as full classes with method implementations and variables. For example, to implement something similar to a comparable interface in C++: class Comparable { public: virtual int compareTo(void* x) = 0; }; class Foobar: public Comparable { public: virtual int compareTo(void* x) { /*compare implementation here */ } //rest of class }; That would be pretty much the same thing as interface inheritance in Java. Any function that takes a Comparable pointer or reference will also be able to take Foobar. In C, you'd have to use function pointers to achieve the same effect. Basically in your structure, you'd have to have a pseudo v-table... a bunch of function pointers: struct Comparable { int (*compareTo)(void* x) = 0; }; struct Foobar { int (*compareTo)(void* x); //rest of structure int i; int j; int k; }; void InitFoobar(struct Foobar* this) { this->compareTo = /*compare to function*/; //rest of the initialization this->i = 0; this->j = 1; this->k = 2; } If you want to pass Foobar into a function that takes a Comparable pointer, just cast the Foobar pointer to a Comparable pointer. The C standard guarantees structures Comparable and Foobar will be laid out exactly the same as long as the variables are declared in the same order. So the first 4 bytes, the function pointer, will be at the exact same offsets in both Comparable and Foobar. However, after the first 4 bytes, Foobar will have extra 12 bytes of stuff where Comparable will not. This memory arrangement lets you treat Foobar exactly like a Comparable. This is also how single inheritance is normally implemented in most languages.


What is the use of interfaces extension through inheritance?

Interfaces are extremely useful in creating inheritance hierarchies in java programs. Interfaces cannot be extended but they will be implemented. Ex: public Interface Interface1 { public String getName() {}; } public Interface Interface2 { public int getAge() {}; } public class InterfaceExample implements Interface1, Interface2 { public String getName() { return "Anand"; } public int getAge() { return 28; } } Here I have implemented two interfaces in a class. I must provide implementation for all the methods that are declared inside both interfaces. So for simplicity I have just written 1 method in each interface but in practical situations you may have numerous methods inside each interface.


Interface devices communicate with the computer through input and output units What are these interface devices called?

Peripherals


What is the difference between cytosplasmic and mendelian inheritance?

To make this simple cytoplasmic inheritance is the inheritance of genes in organelles such as mitochondria that do not go through regular mitosis which is Mendelian inheritance. It is a bit more complex than this and can be easily Googled.


What switching technique allows you to funnel traffic belonging to more than one VLAN through a single switch interface?

The answer is 'Trunking'. The definition of trunking is = The aggregation of multiple logical connections on one physical connection between connectivity devices. In the case of VLANS, trunking allows data from multiple VLANS to share interface on a switch. Network + Guide To Networks Review Question Chapter 6 #18