Docsity

object oriented programming with c++ and java, Lecture notes of Object Oriented Programming

Partial preview of the text

Download object oriented programming with c++ and java and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Object Oriented Programming

Lecture

Inheritance I

©2011, 2014. David W. White & Tyrone A. Edwards School of Computing and Information Technology Faculty of Engineering and Computing University of Technology, Jamaica Email: [email protected], [email protected]

Object Oriented Programming

Expected Outcome At the end of this lecture the student should be able to: ●Explain how inheritance facilitates software reuse ●Identify candidates for inheritance relationships during object-oriented analysis ●Represent inheritance relationships in UML ●Represent inheritance relationships in code

Object Oriented Programming

Topics to be covered in this lecture: ●Identifying inheritance relationships during object- oriented analysis ●Inheritance relationships in UML ●Inheritance relationships in code

Inheritance: Software Reuse

●Inheritance describes a relationship in which child classes derive members from their parent classes ●Inheritance is a key concept of OOP ●The state (attributes) and the behaviours (methods) of the parent class are inherited by the child class ●Can be used to eliminates redundant code ●Promotes software reuse

Inheritance: Software Reuse

Inheritance, through software reuse: ●Reduces software development time

  • Less time is spent writing code ●Reduces software development cost
  • Less code implies less cost ●Increases software reliability
  • If tried, tested and proven parent classes are reused through inheritance, then child classes will inherit the same tried, tested, proven code.

Inheritance: Base and Derived classes

●The general members that apply to a set of classes in an inheritance relationship are placed in the parent class called the base ●Hence, the parent is called a generalized class ●Each child class inherits from the parent class and may customize itself to differentiate itself from other child classes, hence it is called a specialization or derived class ●Other synonyms exist for parent and child

Inheritance: Single vs Multiple

●Single inheritance occurs when a child inherits from only one parent ●Multiple Inheritance occurs when a child inherits from more than one parent

Inheritance: Single vs Multiple

●Multiple inheritance can cause problems with ambiguity if the same method name is inherited from more than one parent (which one will the child use?) ●C++ supports single and multiple inheritance ●Java only directly supports single inheritance but interfaces can simulate multiple inheritance

Inheritance: Role of Access Specifiers

Private ●The members of the parent class are inherited but cannot be accessed by the child class Public ●The members of the parent class are inherited and can be accessed by the child class and other classes Protected ●The members of the parent class are inherited and can only be accessed by the child its descendants

Inheritance: Identifying the Relationship

●When conducting Object-Oriented Analysis (OOA), terms such as “is a” hint at a possible inheritance relationship ●For this reason, inheritance is also called an “is-a” relationship or a generalization

Inheritance: Identifying the Relationship

If a set of classes are very similar, containing common attributes and/or methods: ●Define a parent class ●Place the common attributes and methods in the parent class ●Place only the attributes and methods that make each child class unique in their respective class

Inheritance: Identifying the Relationship

Example: A dialog box has a title, x and y coordinates, height, and width, and some message text. It has click close, click maximize, click minimize and click ok operations. A modal form has a title, warning level and some message text. It has click ok and beep operations. ●Perform an OOA on the above to identify the classes, attributes, methods and relationship

Inheritance: Identifying the Relationship

Class: window ●Attributes: title, and message text ●Method: clickOk

Inheritance: Identifying the Relationship

Parent class: window Child classes: modal form, dialog box ●Classes modal form and dialog box inherit the title and message text attributes and the clickOk method from the window class.