How can interfaces replace the need for multiple inheritance when have existing classes

后端 未结 10 997
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:48

First of all... Sorry for this post. I know that there are many many posts on stackoverflow which are discussing multiple inheritance. But I already know that Java does not

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 01:13

    Just wondering if one could not simply use inner (member) classes (LRM 5.3.7)? E.g. like this (based on the first answer above):

    // original classes:
    public class Tagged {
        // ...
    }
    
    public class XMLElement {
        // ...
    }
    
    public class TaggedXmlElement {
        public/protected/private (static?) class InnerTagged extends Tagged {
          // ...
        }
    
        public/protected/private (static?) class InnerXmlElement extends XMLElement  {
            // ...
        }
    
    }
    

    This way you have a class TaggedXmlElement which actually contains all elements from the two original classes and within TaggedXmlElement you have access to non-private members of the member classes. Of course one would not use "super", but call member class methods. Alternatively one could extend one of the classes and make the other a member class. There are some restrictions, but I think they can all be worked around.

提交回复
热议问题