100 likes | 121 Views
Understand the basics of object-oriented design: how data is stored and processed in objects, their attributes and behaviors, classes as blueprints, inheritance, polymorphism, encapsulation, and abstraction.
E N D
Justin Callanan CS 597 Object-Oriented Design
Object-Oriented Basics • Data is stored and processed in “objects” • Objects represent generalized real world things • Objects have attributes and behaviors • Attributes describe the object • Behaviors change or act on attributes
Using Objects as Representations • Objects typically correspond to real world entities • Examples: • Dog object, with: • An attribute for its breed • A behavior called “bark” • Cat object, with: • An attribute for its color • A behavior called “meow”
Classes and Objects • Classes serve as the blueprint for objects • This blueprint is used to create instances • Each instance of an object has its own data • All data for an object is stored within it • Example: • Instance of the Cat object, with the color “grey” • Instance of the Cat object, with the color “orange”
Generalizing and Specializing • Objects range from abstract to specific • Classes can be derived from one another • Derived classes are always more specific • Derived classes are called “subclasses” or “child classes” • Subclasses are derived from a “superclass,” or “parent class” • Example: • Animal superclass • Dog & Cat subclasses derived from Animal
Inheritance • Subclasses “inherit” attributes and behaviors from superclasses • Subclasses can have additional attributes and behaviors • Inherited behaviors are identical unless “overridden” • Overriding behaviors replaces their internal logic • Example: • Animal superclass with attribute “weight” and behavior “speak” • Cat subclass with overridden “speak” behavior • Dog subclass with overridden “speak” behavior
Polymorphism • Polymorphism is the ability to use subclasses in place of their superclass • Example: • A piece of code uses Animal’s “speak” behavior • The code is given a Cat or Dog object • The code runs, using Cat’s or Dog’s “speak” behavior
Encapsulation • Classes contain all data associated with an object • Data is kept hidden from outside entities • Object data is retrieved through “accessor” behaviors (“getters”) • Object data is changed through “mutator” behaviors (“setters”) • Accessors and mutators can contain unique internal logic
Abstraction • Abstract classes can be used as superclasses • Abstract classes cannot be used to create objects • They serve only as a consistent reference point • They can have incomplete implementations • Code can be written generically with abstract classes • Example: • Abstract Animal superclass • Non-abstract Cat and Dog subclasses (derived from Animal)
Other important points • Objects can contain other objects • Objects can interact with one another • Not all object data is hidden • Behaviors can accept input, provide output, do both, or neither