0% found this document useful (0 votes)
42 views

CSI247 - Lec01 - Objects

This document provides an overview of object-oriented programming concepts including inheritance, polymorphism, abstraction, encapsulation, and interfaces. It discusses how inheritance allows subclasses to inherit properties and methods from parent superclasses. Polymorphism allows objects to take on multiple forms through dynamic dispatch. Interfaces define common behaviors without implementation details, and abstract classes provide partial implementations that must be extended. Generics allow classes and methods to operate on parameterized types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

CSI247 - Lec01 - Objects

This document provides an overview of object-oriented programming concepts including inheritance, polymorphism, abstraction, encapsulation, and interfaces. It discusses how inheritance allows subclasses to inherit properties and methods from parent superclasses. Polymorphism allows objects to take on multiple forms through dynamic dispatch. Interfaces define common behaviors without implementation details, and abstract classes provide partial implementations that must be extended. Generics allow classes and methods to operate on parameterized types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CSI247: Data Structures

Dr. O J Makhura
Computer Science, UB
Block232, Offices 210

Lecture 01: Advanced OO Programming


Lecture outline

1. Objects
2. Inheritance
3. Abstraction
4. Polymorphism
5. Reusability
6. Encapsulation
7. Modularity
8. Interfaces
9. Abstract classes
10. Generics

CSI247: Data Structures 2


Object-Oriented Design Goals

• Robustness
– Unexpected inputs
• Adaptability
– Evolve over time
– Portability (Minimal change across systems)
• Reusability
– Same code, different situations

CSI247: Data Structures 3


Object-Oriented Design Principles

• 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 ….

CSI247: Data Structures


Inheritance Example

CSI247: Data Structures


Inheritance Implementation

• Constructors not inherited


• Access them using super (similar to this)
• Subclass can override methods from super class

CSI247: Data Structures


Polymorphism

• 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( )

CSI247: Data Structures


Polymorphism continued …

• 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

CSI247: Data Structures


Inheritance Example

CSI247: Data Structures


Progression Superclass

CSI247: Data Structures


ArithmeticProgression Subclass

CSI247: Data Structures


GeometicProgression Subclass

CSI247: Data Structures


FibonacciProgression Subclass

CSI247: Data Structures


Interfaces and Abstract Classes
• Objects interaction
– they must “know” about the various messages t
hat each will accept
– application programming interface (API)
• Remember Scanner?
• Interfaces
– Define what an object can do
– Must be implemented by “Concrete” class
• Abstract Classes
– Provide basic implementations
– Cannot be instantiated.
– Must be extended by a “Concrete” class
CSI247: Data Structures
Interfaces

• 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.

CSI247: Data Structures


Sellable Interface

CSI247: Data Structures


Photograph class

CSI247: Data Structures


Transportable Interface

CSI247: Data Structures


BoxedItem Class

CSI247: Data Structures


Interface Inheritance

CSI247: Data Structures


Abstract Classes

• Somewhere between traditional class and interface


• Must have abstract in the declaration
• Can define methods with no bodies (Declared with
abstract)
• Can have instance variables
• Can have concrete methods
• Cannot be instantiated
• Must be extended by a concrete class
• Extending subclass MUST implement all the abstra
ct methods.

CSI247: Data Structures


Abstract Classes

CSI247: Data Structures


Object Casting

• Widening Conversion: occurs when a type T is con


verted into a “wider” type U.
– T and U are class types and U is a superclass of T.
– T and U are interface types and U is a superinterface of T.
– T is a class that implements interface U.
– CreditCard card = new PredatoryCreditCard(...);
• Narrowing Conversion: occurs when a type T is co
nverted into a “narrower” type S
– T and S are class types and S is a subclass of T.
– T and S are interface types and S is a subinterface of T.
– T is an interface implemented by class S.
– PredatoryCreditCard pc = (PredatoryCreditCard) card;

CSI247: Data Structures


Casting Exception

• Can only cast up or down the hierarchy


• Cannot cast across the hierarchy
• ClassCastExcetion (More on exceptions later)

• Use instanceof

CSI247: Data Structures


Interface Casting

CSI247: Data Structures


Generics

• Want a class to save any pair of java objects …

• ObjectPair bid = new ObjectPair("ORCL", 32.07);


• String stock = bid.getFirst( ); // illegal; compile error
• String stock = (String) bid.getFirst( ); // narrowing cast: Obje
ct to String

CSI247: Data Structures


Generics: Formal Parameters

• define a class in terms of a set of formal type paramet


ers
• A and B are formal parameters
• Define generic datatypes (No class implementation
s)

CSI247: Data Structures


Generics: Formal Parameters …

• Declare variable with actual type parameters


• Pair<String,Double> bid;
• The actual types for generic programming must be
object types
• bid = new Pair<>("ORCL", 32.07);
• Type inference
• bid = new Pair<String,Double>("ORCL", 32.07);
• No need for casting now …
– String stock = bid.getFirst( );
– double price = bid.getSecond( );

CSI247: Data Structures


Generics: Methods

• Can create generics at method level


• Add formal parameters in method modifiers

• use of the <T> modifier to declare the method to be


generic
• GenericDemo.reverse(books)
• type inference determining the generic type
CSI247: Data Structures
Bounded Generics

• Remember Sellable interface?


• ShoppingCard class for sellable items
– class ShoppingCart<T>
– BUT: T can be anything
• Use bounded generics
• class ShoppingCart<T extends Sellable>
• Only objects of type Sellable are accepted
– Implementing the interface
– Extending the interface

CSI247: Data Structures


Excercises

• Chapter 2 of the textbook

CSI247: Data Structures

You might also like