Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.

Similar presentations


Presentation on theme: "Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented."— Presentation transcript:

1 Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented Analysis & Design Inheritance

2 Instructor: Tasneem Darwish2 Abstract classes  An abstract class is a class with at least one abstract method  the abstract method may be introduced on the class itself, or it may be inherited from a superclass  the names of the abstract classes are in italics

3 Instructor: Tasneem Darwish3 Abstract classes  Abstract classes have the following advantages: 1.They permit richer and more flexible modelling 2.They lead to more code sharing.  Most object-oriented languages stop us creating instances of abstract classes.  For example, a Java compiler won’t compile any program containing the expression new List().

4 Instructor: Tasneem Darwish4 Abstract classes  When designing a class hierarchy, you should bear in mind that most superclasses are abstract.  This follows from the fact that inheritance hierarchies are naturally derived from the bottom up: 1. We look for the concrete concepts that exist in our problem domain and reason about their knowledge and behavior. 2. We look for commonalities between the concrete classes so that we can introduce more general superclasses. 3. We group superclasses into more superclasses, until we arrive at our most general root class

5 Instructor: Tasneem Darwish5 Abstract classes  When designing a class hierarchy, you should bear in mind that most superclasses are abstract.  This follows from the fact that inheritance hierarchies are naturally derived from the bottom up: 1. We look for the concrete concepts that exist in our problem domain and reason about their knowledge and behavior. 2. We look for commonalities between the concrete classes so that we can introduce more general superclasses. 3. We group superclasses into more superclasses, until we arrive at our most general root class

6 Instructor: Tasneem Darwish6 Redefining methods  Object orientation allows us to redefine elements that we inherit. 1.redefinition allows a subclass to change the implementation of an inherited method 2.redefinition allows a subclass to turn a private message into a public message 3.Redefinition allows us to change the name or type of an attribute

7 Instructor: Tasneem Darwish7 Redefining methods  There are three good reasons for redefining a method: 1.The inherited method was abstract and we want to make it concrete for example, contains is abstract on Collection but we need it to be concrete on Bag and List. 2.The method needs to do some additional work in the subclass 3.We can provide a better implementation for the subclass for example, if we add an index to our LinkedList class, we can redefine contains to be faster than the linear algorithm used by List.

8 Instructor: Tasneem Darwish8 Redefining methods  Every object-oriented language allows a redefined method to invoke the one on its superclass

9 Instructor: Tasneem Darwish9 Implementing a stack class  what we need is a Stack class with the following four messages:  push() to add an object to the top of the stack.  peek():Object to return the object on the top of the stack.  isEmpty():boolean to return true if there are no objects on the stack.  pop():Object to remove an object from the top of the stack and return it.

10 Instructor: Tasneem Darwish10 Implementing a stack class  Suppose that you already have a LinkedList class and you want to reuse it in implementig the stack class:  the LinkedList class has four messages that we should be able to reuse: addElement() which adds an object to the end of the list. lastElement():Object which returns the object at the end of the list. numberOfElements():int which returns the number of objects in the list. removeLastElement() which removes the object at the end of the list.

11 Instructor: Tasneem Darwish11 Implementing a stack class  To incorporate the existing LinkedList behavior into our new Stack class. We could do it in two ways: 1.by inheritance 2.or by composition

12 Instructor: Tasneem Darwish12 Implementing a stack class using inheritance  To make our new class a subclass of LinkedList, we define our stack messages in terms of the inherited messages.

13 Instructor: Tasneem Darwish13 Implementing a stack class using inheritance  Since Stack is a subclass of LinkedList, all other LinkedList messages are also available to stack objects.  Our problem is that LinkedList has messages, such as firstElement, that are inappropriate for stacks, for example:

14 Instructor: Tasneem Darwish14 Implementing a stack class using Composition  Composition is a strong aggregation where the composed object is inside a single composite.  In UML, in order to emphasize that composition is stronger than aggregation, we use a black diamond instead of a white one.  Using composition the Stack is implemented with an internal reference to a LinkedList.  aStack delegates its behavior to aLinkedList.  Since aLinkedList is encapsulated in aStack, the only reference to it, is inside aStack.

15 Instructor: Tasneem Darwish15 Implementing a stack class using Composition  This class provides all the messages that a stack needs, but it does not work with

16 Instructor: Tasneem Darwish16 Inheritance versus Composition  inheritance suffers from the following problems: 1.It’s difficult to do well. 2.It’s difficult to change when you discover deficiencies in your design. 3.It’s more difficult for client programmers to understand.  Composition achieves the same end result as inheritance However, it has the following advantages: 1.It’s simpler to produce. 2.It’s easier to change. 3.It’s easier for clients to understand. 4.It doesn’t leak into client code.

17 Instructor: Tasneem Darwish17 Inheritance versus Composition  for beginners, composition wins over inheritance: it’s reasonable to implement a sizeable application with no inheritance at all.  Inheritance is best left to the experts, especially for when they’re implementing large libraries of reusable code

18 Instructor: Tasneem Darwish18 Multiple Inheritance

19 Instructor: Tasneem Darwish19 Multiple Inheritance  With single inheritance, a class is only allowed to have one parent.  But multiple inheritance, where each class has any number of parents, is also possible. ( Java does have a form of multiple inheritance, but only for interfaces, explained in Chapter 8.)

20 Instructor: Tasneem Darwish20 Multiple Inheritance  Advantages of multiple inheritance are that: 1.It is powerful. 2.It permits private inheritance. 3.It is closer to the real world. 4.It allows mix-in inheritance.  Private inheritance allows one class to inherit from another without the inherited elements becoming part of the new interface (the inherited elements are considered private).  For example: the Stack inherit LinkedList privately and the methods of LinkedList can be accessed from the Stack methods only, not from the Stack object

21 Instructor: Tasneem Darwish21 Multiple Inheritance  Private inheritance is not available in all languages  Some languages (such as Eiffel and C++) permit private inheritance.  Private inheritance is usually provided as a side effect of multiple inheritance, which is also not supported by many languages.  Mix-in inheritance is a design style in which we maintain a single inheritance backbone for our principal classes, while permitting individual classes to inherit from one or more of principle classes.

22 Instructor: Tasneem Darwish22 Multiple Inheritance  Disadvantages of multiple inheritance are that: 1.It introduces complexity 2.It causes name clashes. 3.It causes repeated inheritance. 4.It makes compilers more difficult to write. 5.It makes (fast) run-time systems more difficult to write. Used for polishin g Made in Poland

23 Instructor: Tasneem Darwish23 Multiple Inheritance  A summary of the multiple inheritance facilities available in common languages: ►Eiffel provides straightforward multiple inheritance, plus private inheritance and mix-in inheritance. ►Smalltalk provides only single inheritance. ►C++ provides some multiple inheritance facilities. However, the facilities are incomplete, poorly designed and poorly implemented, so they should be avoided. ►Java provides single inheritance for classes but multiple inheritance for interfaces (abstract classes that have no methods).

24 Instructor: Tasneem Darwish24 Guidelines for using Inheritance  Don’t overdo it: Don’t think that you have to use inheritance a lot, or even at all.  Remember that there are alternatives, such as composition and the use of attributes (for example, a Car class with a color attribute is probably better than three classes called Car, RedCar and BlueCar).  A class should be ‘a kind of ’ its superclass(es): For example, Airplane is not a kind of LandVehicle  A class should be an extension of its superclass(es): In a subclass, make sure that you only add new features.


Download ppt "Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented."

Similar presentations


Ads by Google