190 likes | 203 Views
Learn the relationships between classes, Unified Modeling Language, polymorphism, and more in object-oriented design solutions with multiple classes. Understand associations, aggregations, compositions, inheritance, and operation overriding.
E N D
Chapter 12 Object-oriented design for more than one class
Objectives • To describe relationships between classes • To introduce a simplified unified modelling language • To introduce polymorphism and operation overriding in object-oriented design • To develop object-oriented design solutions to problems using more than one class
12.1 Further object-oriented design
Further object-oriented design • Major advantage of object-oriented programming language is their usefulness in constructing large programs • In designing programs that use multiple classes, need to consider the relationship between the classes and therefore between the objects that are instantiated from those classes
Further object-oriented design • Notations • Notation called standing for united modelling language (UML) • UML allows a designer to represent the relationship between classes as well as between objects
Further object-oriented design • Relationship between classes • Three types of relationship between classes: • Relationship between two classes that are independent of each other but one class might use the services the other provides – Association • Class may be made up of other classes or contain other classes that are part of itself – Aggregation or Composition • Class may inherit all the attributes and operations of a parent class but is given a unique name - Generalisation
Further object-oriented design • Association • Association between two classes is required when the classes need to interact or communicate for the program to achieve its purpose • Association are also called links
Further object-oriented design • Aggregation and composition • Special forms of association where objects of one class, the container class, are made up or consist of other objects from other classes, the components classes • There are whole-part associations: one class is the whole that is made up of parts
Further object-oriented design • Mildest form called aggregation, the part or component classes that make up the whole are able to exist without necessarily being part of the aggregation • Strongest form called composition, the component classes can only exist during the lifetime of the container object and cannot exist outside the composition
Further object-oriented design • Generalisation • Class hierarchy that lets us group the shared attributes and operations into a top-level class, and then to define one or more lower-level classes with extra or different attributes and operations • The top-level class, also called the parent class or superclass, has shared attributes and operations, and the child classes or subclasses, inherit these, adding their own attributes and operations to make them distinct
Further object-oriented design • Polymorphism • Describes the use of operations of the same name for variety of purposes • Operating overrides occurs when a parent class provides an operation, but the inheriting child class defines its own version of that operation • In another type of polymorphism, overloading, several operations in a single class can have the same name
Further object-oriented design • Operation overriding • Occurs when a parent class provides an operation but the inheriting child class defines its own version of that operation • The operation in a subclass will override the operation in the superclass
Object-oriented design with multiple classes • Operation overloading • Occurs when several operations in a single class have the same name but will act differently according to the number of parameters that are passed to the operation when it is called
Object-oriented design with multiple classes • Scope of data • As soon as a project is created, the data that an object needs is brought within its scope • Attributes values are available to all the operations within the class and are visible to each operation
12.2 Steps in creating an object-oriented solution using more than one class
Steps in creating an object-oriented solution using more than one class • Four steps to follow to create an object-oriented solution: • Identify the classes and their attributes, responsibilities and operations • Determine the relationship between the objects and those classes • Design the algorithm for the operation, using structured design • Develop a test or driver algorithm to test the solution
Summary • It is important to understand the relationships between classes. • An association relationship occurs when two classes are independent of each other, but one class uses the services that the other provides. • The relationship between a parent class and a child class is called generalisation.
Summary • Polymorphism refers to the same method name being used in different classes to perform a variety of purposes. • Operation overriding occurs when a parent class provides an operation, but the inheriting class defines its own version of the operation. • Operation overloading occurs when several operations in a single class have the same name, but will act differently according to the number of parameters that are passed to the operation when it is called.
Summary • There are four steps for designing a solution to a simple multiple-class problem: • Identify the classes and their attributes, responsibilities and operations. • Determine the relationship between the objects of those classes. • Design the algorithms for the operations using structured design. • Develop a test or driver algorithm to test the solution.