Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object-Oriented Design: Applying Principles of Inheritance and Classes in Computer Science, Papers of Computer Science

An overview of object-oriented design, focusing on classes, inheritance, and their applications. It covers the definition, properties, and forms of inheritance, as well as the process of applying object-oriented design to real-world problems. Students will learn how to identify classes and messages in problem statements and design classes accordingly.

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-0nz
koofers-user-0nz 🇺🇸

10 documents

1 / 12

Toggle sidebar

Related documents


Partial preview of the text

Download Object-Oriented Design: Applying Principles of Inheritance and Classes in Computer Science and more Papers Computer Science in PDF only on Docsity! 1 CMSC 132: Object-Oriented Programming II Object-Oriented Design II Department of Computer Science University of Maryland, College Park Overview Object-oriented design Objects, methods ⇒ Last lecture Classes, inheritance ⇒ This lecture Applying object-oriented design 2 Elements of Object-Oriented Design Objects Entities in program Methods Functions associated with objects Classes Groups of objects with similar properties Inheritance Relationship between classes Classes Definition Group of objects with same state & behavior Abstract description of a group of objects Similar to data types Type is a set of data values & their operations Example ⇒ integer, real, boolean, string Can view classes as types for objects 5 Inheritance Inheritance forms a hierarchy Helps organize classes Inheritance is transitive Class inherits state & behavior from all ancestors Inheritance promotes code reuse Reuse state & behavior for class Inheritance Hierarchy Example Classes Thermostat Analog thermostat Digital thermostat Programmable thermostat 6 Forms of Inheritance Specification Defines behavior implemented only in subclass Guarantees subclasses implement same behavior In Java → abstract method in superclass Specialization Subclass is customized Still satisfies all requirements for parent class In Java → override method Specialization Example 7 Forms of Inheritance Extension Adds new functionality to subclass In Java → new method Limitation Restricts behavior of subclass In Java → override method, throw exception Combination Inherits features from multiple superclasses Also called multiple inheritance Not possible in Java In Java → implement interface instead Multiple Inheritance Example Combination AlarmClockRadio has two parent classes State & behavior from both Radio & AlarmClock Superclasses 10 Finding Classes Decision not always clear Possible to make everything its own class Approach taken in Smalltalk Overly complex 2+3 = 5 vs. NUM2.add(NUM3) = NUM5 Impact of design More classes ⇒ more abstraction, flexibility Fewer classes ⇒ less complexity, overhead Choice (somewhat) depends on personal preference Avoid making functions into classes Examples – class ListSorter, NameFinder 2) Finding Messages Thermostat uses dial setting to control a heater to maintain constant temperature in room Verbs Uses Control Maintain 11 Finding Messages Analyze each verb Does verb represent interaction between objects? For each interaction Assign methods to classes to perform interaction Analyzing Verbs Uses “Thermostat uses dial setting…” ⇒ Thermostat.SetDesiredTemp() Control “to control a heater…” ⇒ Heater.TurnOn() ⇒ Heater.TurnOff() Maintain “to maintain constant temperature in room” ⇒ Room.GetTemperature() 12 Example Messages Room Thermostat Heater GetTemperature() TurnOn() TurnOff() SetDesiredTemp() Resulting Classes Thermostat State – DialSetting Methods – SetDesiredTemp() Heater State – HeaterOn Methods – TurnOn(), TurnOff() Room State – Temp Methods – GetTemperature()
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved