compsci230s2c 2012 software design and · compsci230s2c 2012 software design and construction...
TRANSCRIPT
Object-Oriented Programming – Key Concepts
COMPSCI230S2C 2012Software Design and Construction
Agenda & Reading
COMPSCI 230Handout 022
Topics: Object-Oriented Programming Abstraction and information hiding Division into parts Inheritance and polymorphism Composition Vs Inheritance
Reading Java 2, The Complete Reference, Chapter 6, “Introducing Classes” The Java Tutorial
http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html http://java.sun.com/docs/books/tutorial/java/javaOO/objects.html http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
Exercise: 02
Object-oriented Approach
COMPSCI 230Handout 023
The object-oriented approach to software development aims to: Manage the complexity of large non-trivial systems Produce change-resilient software Promote reuse of existing code
Key skills of object-oriented developers are: Creativity Intelligence An ability to use abstractions
3. Object-oriented Programming
COMPSCI 230Handout 024
Abstraction An abstraction is a view or representation of an entity that includes only
the attributes of significance in a particular context Abstraction is essential when working with complex systems Without abstraction, the programmer faces an overwhelming level of
detail Examples:
ComplexObject
IdealisedModel
IgnoreInessential Details
Instance variables:Names : StringDateOfBirth : Date
Methods:getSurname(): StringgetFirstname (): StringgetAge(): IntegersetSurname(name: String): void
Customer
Instance Variables:item: Vector
Methods:push(object:Object): voidpop(): Object
Stack
Instance Variables:topLeftCorner: PointWidth: IntegerHeight: Integer
Methods:paint(): voidRectangle
3. Object-oriented Programming
COMPSCI 230Handout 025
Information Hiding Hides unnecessary implementation details from the object user Internal data should not be directly accessible from an object instance
Use Protected or Private access modifiers Use Accessor or Mutator methods to access instance variables (get or set)
Ball b1
xPos:10
yPos: 20
Color: Red
3. Object-oriented Programming
COMPSCI 230Handout 026
Inheritance Ability to create new classes from existing classes Allows reuse of implementation Studies the type-of relationship between derived classes and base class
Classical Inheritance: “is-a” relationship Example: fruits and types of fruit (an apple is a type of fruit)
Polymorphism Different objects can respond differently to the same message Polymorphism is a very powerful mechanism, and it is at the very heart of
inheritance
3. Object-oriented Programming
COMPSCI 230Handout 027
Steps Identify the problem OO Analysis: Define all the types of objects and relationships between
them How?
Building whole systems from parts Identifying commonalities among different entities May use Composition/aggregation or Inheritance
4. Composition/Aggregation Combine simple objects or data types
into more complex ones Whole/part relationship or “has-a”
relationship Many books use terms interchangeably Composition:
When an object is destroyed, other objects belonging to it should be destroyed as well
Examples: Car – engine (Every car has an engine) Vector – elementData[]
Aggregation: Aggregation differs from ordinary
composition in that it does not imply ownership
Car – passengers (Cars sometimes have passengers)
COMPSCI 230Handout 028
Example: Car.java & Engine.java
Inheritance
COMPSCI 230Handout 029
Generalisation Look for conceptual commonalities in the abstractions
Common attributes/state Common methods or behaviour
“is-a” relationship Subclass “is-a” kind of Superclass
Superclass (base class) defines the general state and behaviour Subclass (derived) class : Created from an existing class, by extending the existing class, rather than
rewriting it Examples Person
Employee & Customer (Customer “is-a” Person)
Example: Person.java & Employee.java
Composition Vs Inheritance
COMPSCI 230Handout 0210
Means of transportation
Wheeled vehicle Packhorse
Bicycle Automobile
Component
Button Label TextComponent
TextField TextArea
Car
Engine
Cylinders Crankshaft
Body Wheels
Human body
Bone Muscles Nervous system Skin
Composition Vs Inheritance
COMPSCI 230Handout 0211
Suppose we want to create a class to represent circles: Composition or Inheritance?
Circle is a point with a radius? Circle has a point with a radius?
General guideline Inheritance : Is-a Composition : has-a
But there is no rulebook - the objective is clean, understandable and maintainable code that can be implemented in reasonable time
Some more guidelines: Inheritance provides a means for constructing highly reusable components, but
needs to be used very carefully Choose composition first when creating new classes from existing classes. You
should only used inheritance if it is required by your design. If you use inheritance where composition will work, your designs will become needlessly complicated
Composition
COMPSCI 230Handout 0212
import java.awt.Point;public class Circle {
private Point p;private int radius;public Circle (int x, int y, int radius) {p = new Point (x, y);this.radius = radius;
}public double getX() {return p.getX ();
}public double getY() {return p.getY ();
}public int getRadius () {return radius;
}// additional code
}
Circle c1 = new Circle();System.out.println("x=" + c1.getX() + ", y=" + c1.getY());System.out.println("radius=" + c1.getRadius());
Example: Circle_a/Circle.java
Inheritance We could reuse Point class
since it already has code for representing a point position
COMPSCI 230Handout 0213
Circle c1 = new Circle();System.out.println("x=" + c1.getX() + ", y=" + c1.getY());System.out.println("radius=" + c1.getRadius());
import java.awt.Point;public class Circle extends Point {
private int radius;
public Circle() {}public int getRadius () {return radius;
}// additional code
}
Inherited from Point class
Instance variable: radius
Example: Circle_i/Circle.java
Review
COMPSCI 230Handout 0214
Object Oriented Programming Define all the types of objects that are part of the user’s work environment = OO Analysis Define all the additional types of objects involving the user interface and operating environment = OO
Design Write statements that define all types of objects, including their attributes and behaviors = OO
Programming Abstraction
The ability of a language to take a concept and create an abstract representation of that concept within a program
Information Hiding How well does this language hide an object’s internal implementation?
Inheritance How does this language promote code reuse? Is-a relation
Polymorphism How does this language let us treat related objects in a similar fashion?
Composition Has-a relation