• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
kat hok
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about inheritance. It's a school exercise so I'll just copy the code so it's easier to understand my questions.


The complete public interfaces of the classes are shown, but they may have private fields and methods which are not shown.

The first question is that I should list all the behavioural subtyping relationships between the classes. My thoughts are:
  • has(): TotalMap => PartialMap, TotalMap => Set
  • set(): PartialMap => TotalMap
  • lookup(): PartialMap => TotalMap

  • And I' don't know about the constructor? Any thoughts on if it's correct or not? The thing that I'm not sure about, is if the additional postconditions in set() and lookup() strengthens the postcondition or not. That's what I have assumed now, so please correct me if I'm wrong.

    Next question;
    In the following sections assume our language includes the keyword inherits which functions like extends except it does not create a subtype relation but just an inheritance relation. Each of the sections is independent of the others:
    
  • Implement TotalMap using inheritance or subclassing from PartialMap with maximal code reuse and without adding any new fields.
  • Implement PartialMap using inheritance or subclassing from TotalMap with maximal code reuse and without adding any new fields. Assume that Integer is unbounded.
  • Implement Set using inheritance or subclassing from TotalMap with maximal code reuse and without adding any new fields.
  •  Implement Set using inheritance or subclassing from PartialMap with maximal code reuse and without adding any new fields.


  • I'm not asking you to implement all that, but I need some help starting, as I'm not sure which methods can be reused. And since I'm not sure if my assumptions in the first question about behavioral subtyping is correct, it's difficult to know.
    Hope someone takes their time to help me, thanks!
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    kat hok wrote:I have a question about inheritance.

    Actually, you have several . And the problem with posts like this is that it's difficult to know where to begin, because we don't know how much you've covered so far. But here goes:

    1. Your method list looks fine.
    2. Your constructors look fine, providing all you need to do is create an empty "whatever".
    3. Has inheritance been explained to you? If so, you're presumably familiar with the 'extends' keyword in Java. So, it would appear that what they're saying is that, for your language, "inherits" == "extends".

    I have to admit, I'm not quite sure what the "except it does not create a subtype relation but just an inheritance relation" is all about, unless they're saying that you don't have to worry about superclass constructor calls (I suspect so).

    About the only other tip I can give you is to look at the methods and work out which are common, and which aren't (and it would appear that you've already done that); and simply try to avoid repeating the same method in a class that "inherits".

    HIH

    Winston
     
    kat hok
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you're right! I have many questions indeed!
    Thanks for the response. So now I have a suggestion on how to implement with maximal code reuse, if you would look at it and let me know if there's something more I can do, I would be very happy!

    TotalMap extends PartialMap
    I think the only code reuse is that has() can call super.has(); ?

    PartialMap extends TotalMap
    I think code reuse is for both set() and lookup() ? If has() should be reused, it must ensure result == true
    Here the question says: Assume that Integer is unbounded. I'm not sure how that's relevant?

    Set extends TotalMap
    No reuse? Or can the methods include()/exclude() use anything from TotalMap?

    Set extends PartialMap
    Reuse of code in has()
    Include()/exclude() has some of the same postconditions as set() in PartialMap, but I don't think the code can be reused, or?

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!