How too ? Multiple inheritance with Interface ?

I understand that Java does NOT allow Multiple inheritance. I've read many posts trying to understand the interface statement.
I understand that an interface implements a behavior! But can someone please explain how I would inherit Multiple classes (Thread and JPanel) using an interface ?
Many Thanks,
Rob

I understand that an interface implements a behavior!No, it doesn't. It merely defines what methods must be present in a type. A class is what actually implements the behavior for a particular implementation of that type.
But can someone please explain how I would inherit
Multiple classes (Thread and JPanel) using an
interface ?You can't. Thread and JPanel are classes. A given class can only extend one class.
A class can implement multiple interfaces however. So if at least one of Thread or JPanel were an interface, then your class could inherit from both of them. But that's not the case, so you can't.

Similar Messages

  • How does Java achieve multiple inheritance using interfaces

    Java does not allow multiple inheritance through classes as classes might contain methods with same names. what happens if a class implements two interfaces with same method names?
    I am really confused abt this? Can anybody help me out?
    Message was edited by:
    vijkris

    yes to avoid the ambiguous functions which can result due to multiple inheritance of classes like in c++ , java doesn't have this through classes. But if you have same method (both return type and parameter) then java doesn't bother and it won't complain as ultimately only one implementation is possible in deriving class even though method declalaration is there in both the interfaces. If return type changes then it won't compile as it can't overide the both methods as they have same name and different return types. thats why inside interfaces they restricted the implementation of methods so that it can work fine in ambiguous scenarios.

  • Multiple inherited with generics classes

    Hello all, here is my generic observer/ subject code.
    package jstock;
    * @author yccheok
    public interface Observer<S, A> {
        public void update(S subject, A arg);
    public class Subject<S, A> {
        public void attach(Observer<S, A> observer) {
            observers.add(observer);
        void notify(S subject, A arg) {
            for (Observer<S, A> obs : observers) {
                obs.update(subject, arg);
        private List<Observer<S, A>> observers = new CopyOnWriteArrayList<Observer<S, A>>();
    }However, when I try to implements more than one class (single class only is ok), I get the following error :
    /home/yccheok/Projects/jstock/src/org/yccheok/jstock/gui/MainFrame.java:40: org.yccheok.jstock.engine.Observer cannot be inherited with different arguments: <org.yccheok.jstock.engine.RealTimeStockMonitor,java.util.List<org.yccheok.jstock.engine.Stock>> and <org.yccheok.jstock.engine.StockHistoryMonitor,org.yccheok.jstock.engine.StockHistoryServer>
    public class MainFrame extends javax.swing.JFrame implements
    Here is the code which I am implementing the generic classes.
    public class MainFrame extends javax.swing.JFrame implements
    org.yccheok.jstock.engine.Observer<RealTimeStockMonitor, java.util.List<org.yccheok.jstock.engine.Stock>>,
    org.yccheok.jstock.engine.Observer<StockHistoryMonitor, StockHistoryServer>
    May I know how I can avoid the error message?
    Thank you.
    cheok

    However, when I try to implements more than one classMore than one interface, you mean.
    May I know how I can avoid the error message?You can't implement a generic interface more than once.
    http://angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Can%20a%20class%20implement%20different%20instantiations%20of%20the%20same%20parameterized%20interface?

  • Interfaces instead of multiple inheritance?

    I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."
    But I also read contradictory information-There are no method bodies in an interface.
    Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface. That doesn't seem at all like multiple inheritance. Am I missing something?
    It seems that I will have to cut and paste the implementation code from one class to another, and if I change the methods, I have to cut and paste it all over again.
    I've read that interfaces save a lot of time re-writing methods, but how?
    Does this really provide the same capabilities as multiple inheritance, or am I missing something?
    Thanks,
    Pat

    Pat-2112 wrote:
    I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."
    But I also read contradictory information-There are no method bodies in an interface. That's not contradictory.
    Inheritance is about type, which interfaces provide. It is NOT about sharing code, which is all that's lacking by not having multiple inheritance of implementation.
    Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface. That doesn't seem at all like multiple inheritance. Am I missing something? Yup. You're missing the point of inheritance, and the fact that delegation allows you to use an implementation defined in one class in another class.
    It seems that I will have to cut and paste the implementation code from one class to another, Nope.
    public interface Cowboy {
      void ride();
      void draw();
    public interface Artist {
      void sculpt();
      void draw();
    public interface CowboyArtist extends Cowboy, Artist {
    public class CowboyImpl implements Cowboy {
      public void ride() {
       System.out.println("Giddyup!");
      public void draw() {
        S.o.p("Bang!");
    public class ArtistImpl implements Artist {
      public void sculpt() {
        S.o.p("Demi Moore in Ghost. Yum!");
      public void draw() {
        S.o.p("Sketch a picture of a gun.");
    public class CowboyArtistImpl implements CowboyArtist { // or implements Cowboy, Artist
      private final Cowboy cowboy = new CowboyImpl();
      private final Artist artist = new AristImpl();
      public void ride() {
        cowboy.ride();
      public void sculpt() {
        artist.sculpt();
      public void draw() { // uh-oh, what do we do here?
        artist.draw();
        cowboy.draw();
    }The draw method is not relevant to this particular question. It's an example of one of the problems with MI, and I just included it since it usually comes up int these discussions anyway. Ride and sculpt demonstrate the point about delegation.

  • Doubt on Multiple Inheritance

    Hi all,
    I am confused by the java statement that "By using interface we can achieve multiple inheritance bcoz java doesn't support Multiple inheritance".
    Yes.Ok java doent support Multiple inheritance. Now we know that inheritance means that "one object acquires the property of another object".
    So how can it is possible achieve Multiple inheritance by interface.
    Interface that contains just undefined methods like below code.
    interface Member1
         public void eye();
         public void nose();
         public void mouth();
    interface Member2 extends member1
         public void neck();
         public void hand();
         public void stomach();
    interface Member3 extends Member1,Member2
         public void leg();
    class Man implements Member3
         public man()
         Member3 ref=new Man();
         // Here Implements all 7 methods.
    Is the above code defines multiple Inheitance?
    undefined methods are eye,nose,mouth,neck,handand stomach are fall in Interface Member3 .Yes. But Inheritance means that one object acquires the property of another object.Property means that code and data.
    In here, there is no code just declarations of method which is not to be a code .
    So How can we say interface achieve multiple inheritance.
    Please any one explain and clear my doubt with simple example.
    with cheers,
    G.GandhiRaj.

    Multiple inheritance is about aquiring both behavior and attributes from two or more sources. A lot of times, this "is a" relationship is confused with "has a" relationships.
    For example, a Book "has a" Page. A Book "is a" Publication. So multiple inheretance in this instance would come from stating that a Book "is a" Publication and "is a" PaperProduct. In this example, you could redesign your model and state that a PaperProduct inherits from Publication. However, a Book doesn't have to be limited to being a PaperProduct, it can also be an ElectronicProduct, thus inhereting attributes and behaviors from this new class as well. In essence, the Book can exist in two forms simulataneously (as many actually do). So you still have the need for multiple inheritance - perhaps.
    Interfaces define the behavioral aspects of multiple inheritance. You loose the aquisition of attributes from base classes. In many cases this is acceptable. For the first time I recently found a true need for multiple inheritance in one fo my Java apps. Some would say that my data model is poorly designed then. So I tried restructuring the model and that solve the problem.
    It is probably a good idea to completely understand your data model from many dimensions first before resorting to the copying of attributes like I almost did. Don't be locked into a design too quickly. Be willing to change your mind and you will probably find a solution that works.

  • Why java does not support multiple inheritance ???

    Hai friends ..iam new to java .. i have doubt ..plz help me
    Why java does not support multiple inheritance ???

    The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal.
    To understand multiple inheritance, the learner needs some level of expertise like virtual derivations etc in c++. Multiple inheritance will allow method duplication, and throws the learner into confusion which method might be called by the compiler in which scenario at run time.
    Even though this answer seems to be funny, this is the actual reason why java omitted multiple inheritance of classes.
    But java support multiple inheritance 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.

  • Multiple-inheritance semantics

    I saw the other day multiple-inheritance is possible with JavaFX classes.
    class X extends Y,Z,A {
    Something which is possible now as JavaFX classes don't have constructors. Now we can mix-in (as in Scala) code from other classes.
    My question to the people who design this is about their intention: is it meant to be used as a composition ? Or still as a specialization like it is in Java.

    If you allow multiple inheritance you get some ambiguities. Java avoid those by having single inheritance of implementation (one class may extend one class only) in addition to multiple inheritance of interfaces (that don't implement anything). This is much cleaner and that was one of Javas design objectives. The goal is to be safer than C++ and this often means leaving things out, like calls by reference, pointer arithmetics and multiple inheritance.

  • INF Looking for means to dialog C#-Dev team about multiple inheritance.

    Please help.
    I really need multiple inheritance with C#. Is there any forum/means to have a dialog with the dev's for C# about this? The amount of extra work and maintenance costs of not having multiple inheritance has been a big problem, but lately, it has really become
    a burden for not having. The maintainability and reuse-ability of code is drastically reduced without it.

    Btw, I think that this has been discussed
    many times before. Almost once or twice each year since .NET is released (that's 12 years by now).
    I don't think you can make them allow this feature as tons of example trying to convince them this is needed has been proved not necessary to use multiple inheritance by them.
    Many of the discussions ends with something like "Java also does not support multiple inheritance but there isn't seems to be a problem for them". Maybe you can get more luck to convince Oracle to include MI in Java first.
    Btw, I found it hard to believe you need Multiple Implementation Inheritance to... improve maintainability of code? WTF??? I think Multiple Implementation Inheritance has it own place in the hall of fame for the bugs it caused in languages that supports
    it, even in C++.

  • Hi All , Will Java supports Multiple Inheritance  classes???

    Hi All ,
    Will Java supports Multiple Inheritance by classes???
    Thanks in advance,
    Prakash

    No, Multiple inheritance would look like
    public class A extends B,C {(You can do that in C++, but it's rarely a good idea).That's not true at all. It's not inherently harmful, in C++ or any other language. It's entirely possible to do it correctly when it truly makes sense.
    Java just guarantees that nothing bad can happen to you by only allowing multiple inheritance of interface. You can't ever have multiple inheritance of implementation, that's all.
    %

  • How java support multiple inheritance by the use of interface.

    As per my understanding, Interface is just having the signatures of the methods not the implementation.
    So How java support multiple inheritance by the use of interface?
    Answer 1: we can institate interface reference by its implemented
    class.
              ����� interface inf...
              ����� class aa implements inf..
              ����� class bb implements inf....
               Now, inf i = new aa();
               inf i = new bb();
    Answer 2: We can extends as many interface as we want in the
    single
               interface.
               i.e. interface infFirst....
               interface infSecond....
               interface infThird....
               Now ,
               interface ingMulti extends infFrist, infThird...
    By above two answers its not prity clear as per the multiple inheritance in C or C++.
               i.e.
               class first{
               method abc();....}
               class second{
               method bbc()......}
               class multi::first::second{
               we can call to abc();.....as well as bbc();
    -Please give your important suggstion on the same.(Hope I explain it well.)
    -Jeff

    The keyword implement is used only for interfaces not
    for abstract class. If i am wrong correct me.I believe your right, but I will double check.
    As for the multiple inheritence think about the following code:
    class Animal {
        //  Animal generic stuff in this class
    interface Eat {
        //  Generic stuff that models eating behavior
    interface Runs {
        //  generic methods that model running behavior
    public class Horse extends Animal implements Eat, Runs {
        //  Stuff specific to a horse
    }The Animal class is generic but has stuff in it common to all animals.
    The Eat interface models behavior that is generic to eating, all living things have to eat something to survive. Herbavore are different from carnivores.
    The Runs interface models generic behavior to running, such as speed. A cheeta definately runs faster than a human.
    This brings us to the Horse class. It extends the Animal class because it "is-a" animal, and it implements the eat and runs interface because they are behaviors a horse has.
    I hope that helps.
    Extending an abstract class is the same as extending a regular class with the exception you MUST override all abstract methods in the abstract class. Thats not too difficult but I believe when designing classes, designing an abstract can be more diffecult than modeling the base class, and generic behaviors in interfaces. JMO.
    JJ

  • Multiple Inheritance problem persists in Interfaces

    Hi,
    I tentatively made a program and found that multiple inheritance problem of C++ persists even with interfaces. Although this is definetely a special case but I want to know what is this problem known as( i know that this is perhaps known as diamond problem in C++). And is there a way out of this thing.
    interface one
         int i=10;
    interface two
         int i=20;
    interface z extends one,two
    public class xyz implements z
         public static void main(String [] a)
         System.out.println(i);
    }O/P
    D:\Education\Java\JavaStudyRoom\Applets>javac xyz.java
    xyz.java:16: reference to i is ambiguous, both variable i in one and variable i
    in two match
    System.out.println(i);
    *^*
    *1 error*
    Thanks for replying

    suvojit168 wrote:
    I tentatively made a program and found that multiple inheritance problem of C++ persists even with interfaces. Although this is definetely a special case but I want to know what is this problem known as( i know that this is perhaps known as diamond problem in C++). And is there a way out of this thing. This is not the so called diamond inheritance problem. What you have here is an ordinary name clash. And as has been noted you can resolve it by qualifying which constant you're referring to, like
    System.out.println(one.i);
    For the diamond inheritance problem to apply both the one and the two interfaces would need to inherit a common ancestor (that's how the diamond is formed). Furthermore the common anscestor would need to carry implementation which would then be inherited two ways, once via one and once via two. This is the diamond inheritance problem Java is avoiding by allowing single inheritance of implementation only.
    P.S. My previous post was posted my mistake.

  • How do i deal with multiple iPhoto libraries when migrating to Photos

    how do i deal with multiple iPhoto libraries when migrating to Photos

    I would merge them before the migration.  Also, spend some time doing any batch changes of names and dates beforehand too.
    iPhoto Library Manager has good merge and duplicate search facilities.
    http://www.fatcatsoftware.com/iplm/

  • Multiple Servers with same Remote Interface?

    Is it possible to declare the same remote interface on multiple RMI servers? Secondly, is it possible to pick which RMI server will manage a particular request?
    In other words, imagine if multiple servers could service a particular request that has the same signature, but the client wants to specify which server to send the invocation. An example would be the following:
    public String getPhoneNumber(String pLastName) ;But the client should decide that server A should manage requests of last names starting with A-G, server B should handle H-P, and server C should handle Q-Z. The remote method invocation is the same, but the server upon which is should be invoked needs to be determined at runtime.
    I realize this can be done multiple ways, with an interim proxy server that designates the server. However, I'm most interested in the ability within the RMI and/or JNDI standard to select and invoke methods on a particular server.
    Thanks for any help.

    "Server" is a little bit vague in this context.
    My assumption is that you want separate RMI servers running on separate hosts. (Otherwise, why bother with multiple "servers"?)
    That being said, it's not clear to me how you would expect RMI to sort out something as application-specific as you suggest. It sounds to me like you need your own registry/redirector. This is going to either be an rmiregistry rewrite by you, or - as was suggested by another poster - a proxy.

  • How do I use edge commons composition loader to load multiple compositions with a next and back button?

    I am working on an interactive book and have set up each page as a separate composition in edge.
    I am using  the edge commons JS library to load multiple compositions into a main composition.
    You can see how this works here: Edge Commons - Extension Library for Edge Animate and Edge Reflow | EdgeDocks.com
    The way the edge commons tutorial is set up requires a button for each composition i want to load. I am interested in loading multiple compositions with a "next" and "back" button, and a "swipe left, "swipe right" gesture on the content symbol that each composition is loaded into. I also need the swipe features on the content symbol not to interfere with the interactive elements on the loaded composition.
    Please suggest a solution that will work without adding additional scripts beyond edge commons and jquery.

    Sort of. I'm using this code inside an action for a button symbol. But it doesn't work perfectly. Trying to debug it.
    Let me know if you have any luck.
    //Check to see if pageCounter already exists
    if (typeof EC.pageCounter === 'undefined') {
      // it doesn't exist so initialize it to first page
        EC.pageCounter = 2;
    //check if the page is only 1 digit -- patch for single digit
    if (EC.pageCounter < 9) {
       // it is, so we need to pad a 0 on the front.
      EC.pageCounterString = "0" + EC.pageCounter;
      //e.g.  01 ...09,11,12,13....115,222352,,....
    else {
      EC.pageCounterString = EC.pageCounter;
    EC.loadComposition(EC.pageCounterString + "/publish/web/" + EC.pageCounterString + ".html", sym.$("container"));
    EC.pageCounter = EC.pageCounter + 1;
    //TODO for back  -1

  • How to connect my app interface with the code in flash builder?

    I'm a beginner and learning my way around actionscript,mxml and flash builder.So this may seem foolish but please bear with me,I'm creating a very simple ios app using flash builder,I have created a simple app interface (A background and a custom navigation bar)using photoshop for my app,now the question is how do i import it into flash builder so that i can connect it with the code i have written? or is there another simpler way to create a app interface using other products like fireworks or catalyst?
    In short, I want to know how is an app interface created(wt software) and how is it connected with its code using flash builder.Any help would be greatly appreciated, and tutorials would be swell
    Thanks!

    Can you use stage.width or stage.stageWidth?

Maybe you are looking for

  • How do I find the number of users logged on another server?

    I'm trying to ping other servers and find the number of users logged on that server. Any leads?

  • Autoinvoice error - You must supply payment terms for your non-credit trans

    Dear all, I've this error "You must supply payment terms for your non-credit transaction" shown on AR_RA_INTERFACE_ERRORS_ALL table after ran the autoinvoice. In the RA_INTERFACE_LINES table I had inserted this value "14 DAYS" in TERM_NAME field alre

  • FI/CO Production support

    Hi All, I am have an interview scheduled for FI/CO production support job, but I have never assumed the responsibility till date. I know about ticketing process and real-time issues, but for interview sake what areas do I need to brush up? Month-end

  • ESA X1070 - nic pair + vlan

    hello, vlan is possible configure only on physical NIC (ironport esa doc). nic pairing is possible configure betwen two data interfaces (combination rj45 + fiber is possible). I have question about combination vlan + nic pairing (I have no esa in the

  • Http binding (rest service) adapter unable access endpoint

    Hi I have a problem with security. Http binding adapter has endpoint https://192.168.10.10:8080/uuid/453434563 I have added my client certificate and server certificate to keystore. I have made a configuration in soa server/console keystores and ssl.