Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 2 Subclassing.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 2 Subclassing."— Presentation transcript:

1 CSC 205 Programming II Lecture 2 Subclassing

2 Why OO? Software is inherently complex Complexity of the problem domain Difficulty of managing the development process Decomposition The technique of mastering complexity has been known since ancient time: divide et impera (divide and rule) -- Dijkstra

3 OO v.s. Procedure-Oriented
P!!!!rocedural: top-down structure design with each component denote a major step in some overall process OO: viewing the world as a set of autonomous agents that collaborate to perform some higher level behavior Which is better?

4 Procedural Decomposition

5 OO Decomposition

6 Complex Systems – an example
A university Mercer HR Academics Athletics … … CLA Engr. People CSC … … Math … … Student Employee Graduate UnderGrad Admin. Faculty

7 Bring Order to Chaos The role of abstraction The role of hierarchy
An individual can comprehend only about seven (7) chunks of information at one time! Ignore inessential details, deal with the generalized, idealized model of the object The role of hierarchy Object structure: illustrates how different objects collaborate with each other Class structure: highlights common structure and behavior within a system

8 Abstraction Idealized structure model of objects
Recognize similarities Ignore differences (for the time being) For a particular purpose Provide crisply defined conceptual bounders Distinguish an object from other kinds of objects

9 Abstraction

10 Abstraction Characterize the behavior of an object
by considering the services it provides to other objects (or its responsibilities) (client-server) contract model from the outside view Works together with encapsulation Expose what an object can do and how to request for services (which constitute the behavior of an object) Hide implementation details

11 The Base Class: Account
public class Account { // Instance variables private double balance; // Constructors public Account(double initialBalance) { balance = initialBalance; } public Account() { balance = 0.0; // more methods omitted

12 Encapsulation

13 Responsibility Responsibilities of an object include
State: information to store Implemented with variables in Java Operations: Implemented with methods in Java Preconditions: conditions assumed by operations Postconditions: conditions satisfied by operations Invoke operations by message passing

14 Message Passing Three components that comprise a message:
The object to which the message is addressed (YourBicycle) The name of the method to perform (changeGears) Any parameters needed by the method (lowerGear)

15 Hierarchy Hierarchy is a ranking or ordering of abstractions
Two most important hierarchies The “part-of” relationship, or aggregation The “is-a” relationship , or inheritance Inheritance, also known as subclassing Single inheritance: the only legal way in Java Multiple inheritance: supported in C++

16 Hierarchy: “is-a” relationship

17 Illustrating Inheritance
Account specialization generalization SavingsAccount CheckingAccount

18 Subclassing in Java A subclass SavingsAccount:
public class SavingsAccount extends Account { private double interestRate; public double getInterestRate() { return interestRate; } public void setInterestRate(double rate) { interestRate = rate;

19 Writing a Subclass A SavingsAccount object will contain two variables:
balance, and interestRate

20 Writing a Subclass Methods that can be applied to SavingsAccount objects: getInterestRate setInterestRate deposit (inherited) withdraw (inherited) getBalance (inherited) close (inherited)

21 Hierarchy: “part-of” relationship

22 Aggregation & Composition
University aggregation composition Student Department

23 Modularity

24 Typing

25 Typing Supports Polymorphism
Shape +draw() Oval Rectangle +draw() +draw() // Shape s = ShapeBuilder.getNextShape(); s.draw();

26 Concurrency

27 Persistence

28 Elements of Object Model
Abstraction: model the essential attributes Encapsulation: hide implementation details Modularity: (think package in Java) Hierarchy: ranking/ordering of objects Typing*: enforcement of the class Concurrency*: distinguishes active object Persistence*: transcends time and/or space


Download ppt "CSC 205 Programming II Lecture 2 Subclassing."

Similar presentations


Ads by Google