CSI247 - Lec01 - Objects
CSI247 - Lec01 - Objects
Dr. O J Makhura
Computer Science, UB
Block232, Offices 210
1. Objects
2. Inheritance
3. Abstraction
4. Polymorphism
5. Reusability
6. Encapsulation
7. Modularity
8. Interfaces
9. Abstract classes
10. Generics
• Robustness
– Unexpected inputs
• Adaptability
– Evolve over time
– Portability (Minimal change across systems)
• Reusability
– Same code, different situations
• Abstraction
– Fundamental parts
– Abstract data types (ADT)
• What an operation does NOT how it does it
• Encapsulation
– Never reveal internal implementation details
– Freedom of implementation
– Public interface
• Modularity
– Different components
– Proper organisation
CSI247: Data Structures
Inheritance
• Object hierarchy (All classes inherit from Object)
– Is-a concept
– Similar abstract definitions at a level
– Specific to general up the hierarchy tree
• Mathematically
– set of houses is a subset of the set of buildings
– Set of houses is superset of the set of ranches
• New class designed based on another
– Subclass and superclass
– Subclass inherits properties and methods from s
uperclass
– Subclass has one superclass
– Superclass may have many subclasses
CSI247: Data Structures
Inheritance ….
• Many forms
– CreditCard card;
– card = new PredatoryCreditCard(...);
• Liskov Substitution Principle
– a variable (or parameter) with a declared type ca
n be assigned an instance from any direct or indi
rect subclass of that type.
• Card is polymorphic
– card.makePayment(50)
– But not card.processMonth( )
• Dynamic dispatch
– card.charge(100)
– Implemented in CreditCard and PredatoryCredi
tCard
– The implementation to use is decided at runtime
• instanceof
– Check at runtime the actual type
– Boolean operator
– E.g. card instanceof PredatoryCreditCard = true
• Enforce an API
• A collection of methods with no body or data
– Method signatures only
• No constructors
• No fields
• Declared with the key word interface
• A class them implements the interface
• A class can implement multiple interfaces.
• A class must provide implementations of all interfa
ce methods.
• Use instanceof