cs31 lecture 13

Oo languages may have different default

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ful: other OO languages may have different default implementations! Strong advise: do not do multiple inheritance unless you know what you are doing You probably want to use interfaces and not class inheritance 5 Some guidelines to determine a good hierarchy RULE 1) If A : B in the hierarchy, then the sentence “A is a B” should make full sense Example: “a rectangle is a square” vs. “a rectangle is an object” Meaningful design may not lead to maximal code reuse RULE 2) If A : B in the hierarchy, then A must “extend” the behavior/functionalities/properties of B 6 Quick summary How a method/attribute can be accessed outside the current class is determined by public/protected/private Protected attributes/methods can only be accessed from within the class and subclasses of it Private attributes/methods can only be accessed from the class itself Public attributes/methods can be accessed from anywhere How multiple inheritance is handled depends 7 Design question: inheritance vs. attribute? When designing a project, a question often occurs Do I make B inherit from A? Or, do I add an object of type A as attribute of B? This can be solved for instance with the “is a” test Both choices can lead to a valid implementation But design may be less clear in one case vs. the other 8 Examples Let’s design a hierarchy for images 9...
View Full Document

Ask a homework question - tutors are online