The document outlines the principles of Object-Oriented Programming (OOP) and its advantages over pre-OOP coding practices, which often led to poor code reuse and maintainability. OOP emphasizes design before coding, allowing for modular code, clear interfaces, and the ability to easily add new classes. Key concepts include class and object definitions, inheritance, data encapsulation, polymorphism, and abstraction, each contributing to more organized and manageable code development.
Why do OOP? •Pre-OOP Days – Write Code Fast as One Can. • Code was not designed – code smells bad. • Poor reuse of code – lots of copy and paste. • Code became unreadable – spaghetti code. • Difficult to trace – hard to fix bugs. • Fix one bug - breaks something downstream. • Object Oriented Programming (Design) • Design First, then Code. • Promotes reuse of code. • Code becomes modular with clearly defined interfaces. • Easy to maintain and modify. • Add new objects (classes) with small differences to existing objects.
3.
OOP Example Banking Application- we might have checking accounts, savings accounts, money market accounts, and lines of credit. • Some of these accounts would have similar data fields (e.g., account number, balance). • Some of these accounts would have the same actions (e.g., withdraw money, get balance). • Some of these accounts would have some data and actions specific to the account type not shared with the other accounts. Pre-OOP Days : • Write a component (set of functions) for each account. • Common data fields and actions would be duplicated. • When maintaining/debugging, maybe confusing which data (e.g., account number goes with which account (e.g., checking, savings, money market, line of credit).
4.
OOP Principles –Class & Objects • Class – A means to construct objects from predefined specifications (e.g, forms/templates), which may contain: • Initialized Data • Placeholders for Data • Methods for Accessing and Manipulating Data • Object – An instance of a class, i.e., built from a specification A collection of specifications for building Something. Class A Class B Class C Specifications on what to build and how to build it Class A ConstructionRequirements Object Class Constructor Parameters Class Definition Class Instance (Object)
5.
OOP Principles –Inheritance • Class Inheritance – Derived (Sub) Class • Assembly of itself (derived or sub) with one or more other classes (base or super). • Hierarchical Classes – One class inherits a class, which inherits another, etc. Class A Class B Class C Class D A collection of specifications for base (super) classes. A collection of specifications for derived (sub) classes. Data/Methods of D Inherit Data/Methods of class B and C. Car Truck Chassis Wheels Motor Legend: Inheritance Parameters Base (super) classes are inherited Upwards into derived (sub) classes. Parameters are passed downwards From derived (sub) to the constructors of base (super) classes.
6.
Derived Class OOP Principles– Data Encapsulation • Data Encapsulation – i.e., data hiding • Data Accessibility Object A Data is hidden Object B (data) A can see B A cannot see B’s data B can see its data Object B (public) (protected) (private) Object A A can access B’s data A cannot access B’s data A cannot access B’s data Derived Class can access B’s data Inheritance
7.
OOP Principles –Polymorphism • Method Overloading – The same method (function) can have multiple implementations for different parameters. • Operator Overloading – The same operator (e.g., +) can have multiple implementations for different data types. Method Name: A Parameters Parameters Implementation Implementation One (same) name for the Method Switch to implementation based on parameters Parameters specific implementations Operator (e.g., +) Data Type Data Type Implementation Implementation Can be different number and/or data type of parameters. Same operator Switch to implementation based on data type of parameters. Data Type specific implementations
8.
OOP Principles –Abstraction • Abstract Classes – Reduce Complexity by Hiding Details • Has Method Signatures (declarations), but not implementation. • Abstract Methods must be implemented by derived (sub) class. Method Name: A Method Name: B Parameters Parameters Abstract Class Derived Class Method Name: C Parameters Method Name: A Parameters Method Name: B Parameters Implementation Implementation Implementation Inheritance Derived Class specific implementations of inherited abstract methods