Any way to extend two or more classes in java?

前端 未结 12 1453
再見小時候
再見小時候 2021-01-17 02:20

I know that Java does not allow us to extend more than one class. I know that interfaces also exist and I don\'t want to use them this time. Is there some kind of trick or w

相关标签:
12条回答
  • 2021-01-17 02:45

    You can do it easily with interfaces and composition.

    The question is why you would ask such a thing? Why do you not want to use interfaces "at this time"?

    You have to know that Java only allows single inheritance of implementation. You've spent seven months at SO, so surely you must know how to use the search feature. The question has been asked here repeatedly.

    Be more creative. There's no reason that a JPanel has to be Observable. JPanel is for rendering. I agree that its model data might want to be Observable. By all means do it with composition.

    0 讨论(0)
  • 2021-01-17 02:45

    Use interfaces instead. The following is how that would look in code:

    public Class ClassName implements Interface1, Interface2 {
    
        Methods, fields etc...
    
    }
    
    0 讨论(0)
  • 2021-01-17 02:49

    may be this helps

        class One{
        void oneMethod(){
    
        }
    }
    
    class Two extends One{
        void TwoMethod(){
    
        }
    }
    
    class Abc extends Two{
    
        @Override
        void oneMethod() {
            // TODO Auto-generated method stub
            super.oneMethod();
        }
    
        @Override
        void TwoMethod() {
            // TODO Auto-generated method stub
            super.TwoMethod();
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-17 02:51

    You can use a lookup of capabilities.

    class Vehicle {
    
        T <T> lookup(Class<T> klazz) {
            return map.get(klazz); // typically
        }
    }
    
    interface Flyable {
        void fly();
    }
    
    Vehicle x;
    Flyable f = x.lookup(Flyable.class);
    if (f != null) {
        f.fly();
    }
    

    This decouples classes. It requires runtime checking though.

    It uses delegation: local members implementing the lookup'ed class/interface.

    Advantage:

    You can derive a class from Vehicle that can both fly (Flyable) and dive (Diving). The flying can be implemented by an instance of a shared class. You can even dynamically compose capabilities (add wheels to a boat).

    That often is better than implementing several interfaces, where fly() would either be a code copy, or delegate to a Flyable field (with shared implementation class).

    Especially with multiple interfaces/classes involved you otherwise risk code on base class objects (Vehicle here) with cases and unsafe casts, forgetting if (x instanceof Boat).

    0 讨论(0)
  • 2021-01-17 02:54

    No, unlike C++ you cannot extend multiple classes but you can implement multiple interfaces.

    0 讨论(0)
  • 2021-01-17 02:55

    I am going to upset an awful lot of people by saying yes you can - kind of.

    It is certainly not easy and you have to use reflection but here you will find an example that allows you to dynamically extend one object with another. Obviously, once you can do that you can then extend another one too.

    Essentially, it uses the Proxy class to intercept method calls and direct them to one or other of two objects - this implements A overrides B. This is not quite what you are looking for but it could easily be tweaked to detect methods that exist in both classes and call them both - thus achieving A extends B dynamically - kind of.

    Note that you need to read the section entitled Dynamic Object Adapter using Dynamic Proxies section of the link.

    0 讨论(0)
提交回复
热议问题