OOSummary - Mathematical & Computer Sciences

Download Report

Transcript OOSummary - Mathematical & Computer Sciences

Software Engineering Foundations
Summary
Object Oriented Concepts
Dept of
Computer
Science
13/04/2015
Monica Farrow EM G30
email : [email protected]
Material available on Vision
F21SF1 Maps End
1
Key OOP concepts

Basic OOP





OOP, objects, classes
Encapsulation and data/information hiding
Coupling and cohesion
Static
Inheritance




Superclasses and subclasses
Abstract methods and classes
Interfaces
Polymorphism, overloading and overriding.
13/04/2015
2
OOP

OOP – object oriented programming


Read the java tutorial


http://docs.oracle.com/javase/tutorial/java/concepts/
Here is a good summary


A style of designing programs based on ‘objects’
http://www.cis.upenn.edu/~matuszek/cit5912011/Pages/o-o-concepts.html
Also

http://www.ntu.edu.sg/home/ehchua/programming/java
/J3a_OOPBasics.html
13/04/2015
3
Objects


An object is often a real-world object such as a
car.
An object has



state (attributes/properties/data/fields)
e.g. model name, tank size, fuel in the tank
And


Behaviour (operations/functions/methods)
e.g.



How far can it travel on a full tank
Whether the tank is empty or not
The tank can be filled (amount of fuel in the tank altered)
13/04/2015
4
Class

A class is a template for an object


It describes the attributes (instance variables) and
operations (methods) for any object of the class
A class doesn’t define actual objects
Car
model : String
tankSize : int
fuelInTank : double
getModel() : String
getTankSize() : int
setFuelInTank (double fuel): void
Etc etc
13/04/2015
5
Objects (again)


An object is an instance of the class.
There can be many objects of a class


E.g. Person -> Monica Farrow, Hamish Taylor, etc etc
Objects can be instantiated (created)
13/04/2015
6
How a program works
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html
13/04/2015
7
Encapsulation



In English ‘encapsulate’ means to enclose as if in a
capsule.
In OOP, it means that the attributes and
operations for objects of a class are held together.
http://www.c-sharpcorner.com/UploadFile/e881fb/learn-object-orientedprogramming-using-C-Sharp-part-5/Images/Encapsulation.jpg
13/04/2015
F21SF1 Maps End
8
Data hiding / Information hiding

Encapsulation enables data hiding, also called
information hiding.

The internal state of the object (the instance variables)
are hidden from objects of other classes.


These variables are then accessible by providing
accessor (‘get’) methods


Their access modifier is private
Their access modifier is public
Similarly, they can be modified by providing mutator
(‘set’) methods

13/04/2015
Their access modifier is public
F21SF1 Maps End
9
Using information hiding

The objects pass messages to each other using
the public methods.



The method signature is known (method name,
parameters, return type)
This is the interface
The implementation details (what the body of
the code does) is hidden, encapsulated within the
class
13/04/2015
F21SF1 Maps End
10
Coupling

Coupling is about the inter-connected-ness of
different classes.


Loose coupling means that changes in one class should
not usually require changes in other classes.
Information hiding supports loose coupling

E.g. change the way that a name is stored inside the
class, from a single String to 2 Strings for first names
and last names



Change the instance variables and the public method bodies
Do NOT change the method signatures (the public interface)
The classes using the methods do not need altering.
13/04/2015
F21SF1 Maps End
11
Cohesion


Cohesion is about a class having responsibility for
set of closely related tasks.
E.g.



Put a method to get how far a car can travel inside the
Car class
Put a method to get the initials inside the Name class
Don’t put lots of methods to manipulate the owner
name string inside a Car class – they’re not about cars.
13/04/2015
F21SF1 Maps End
12
Coupling and cohesion



An OOP program should have loose coupling and
high cohesion.
Separating entities into separate logical units
makes them modular, easier to code, understand,
analyze, test, and maintain.
The logical units can also be re-used in other
programs.
13/04/2015
F21SF1 Maps End
13
Static variables and constants


A class may contain static variables or constants.
A static constant has the same value for all objects
of the class and never changes E.g.



the number of days in a week
the number of holidays that all Employees can take (if
this will NOT change)
A static variable has the same value for all objects
of the class and might change during the program
run E.g.

number of holidays that all Employees can take (if this
might be changed during a program run)
13/04/2015
F21SF1 Maps End
14
Static methods

A class may contain static methods





These can only access static variables, constants and
other static methods
They often provide useful functionality
They do NOT use instance variables or non-static
methods
E.g. method to return the days of the week
E.g. Math.round, String.format, main method
13/04/2015
F21SF1 Maps End
15
Inheritance

A class can



Get some characteristics (instance variables, methods)
from a parent or superclass. A subclass extends a
superclass. A subclass is a superclass.
Provide its own characteristics specific to itself. It is the
child or sub-class.
E.g.


Superclass Shape specifies the colour. All shapes have a
colour.
Subclass Circle specifies radius, subclass Square
specifies side. These attributes are specific to Circle and
Square.
13/04/2015
F21SF1 Maps End
16
Inheritance
Shape
colour:String
getColour() : String
setColour (String c) :void
Circle
Square
radius:double
side:double
getRadius() : double
getSide() : double
13/04/2015
17
Object class


The Object class is the superclass for all classes
It contains default toString and equals methods,
based only upon class name and location in
memory.
Object
toString() : String
equals(o : Object) : boolean
Shape
Circle
13/04/2015
Square
18
Overriding



Subclasses often provide their own toString and
equals methods.
This is called overriding, the method in the
subclass is used in preference to the method in
the superclass.
The method in the superclass can be called,
using ‘super’ e.g. super.toString().
13/04/2015
19
Overloading

Overloading is using the same operation name
with a different purpose. E.g.



System.out.println(5); //an int
System.out.println(“Hello”); // a String
Overloading is also used for the + operator in
java


int y = 3; int z = y + 2;
String s = “Hel” + “lo”;
13/04/2015
20
Polymorphism


Polymorphism means ‘changing form’
In Java, this means


Overloading – same method name, different
parameters
Overriding – same method name, different subclass
13/04/2015
21
Abstract methods



An abstract method contains no code
An abstract method is a method signature,
defining the interface for a method which must
be supplied by the subclass.
E.g.


Shape superclass could define an abstract method
getArea().
The implementation is in each subclass.
13/04/2015
22
Abstract class

An abstract class contains




Some abstract methods. No implementation. E.g.
Shape getArea()
Some concrete variables and methods, including the
implementation. E.g. Shape colour, getColour().
You can NOT call the constructor of an abstract
class directly
You can create (instantiate) objects of the
subclass. Their constructors contain a call to the
superclass constructor (super() )
13/04/2015
23
Interface

An interface is like an abstract class without the
concrete parts. An interface is a
specification without implementation.




All variables are static
All methods are abstract
A class can implement an interface.
This means that the class MUST provide
(implement) all methods defined by that
interface (e.g. actionPerformed in GUIs)
13/04/2015
24
Multiple inheritance



In Java, a subclass can only extend one
superclass.
In Java, a class can extend any number of
interfaces.
E.g.

class Class extends Superclass
implements Interface1, Interface2, .....
13/04/2015
25