OOP Revision
OOP Revision
OOP Revision
F27SB
Example Implementation
public class Dog
{
//Individual characteristics (instance fields).
FIELDS
private String name;
//constructor
public Dog(String name) {
this.name = name; CONSTRUCTOR
}
//accessor method
private String getName() {
return name;
METHODS
}
}
OOP CONCEPTS
Coupling
• Coupling refers to links between separate units
of a program.
• If two classes depend closely on many details
of each other, we say they are tightly coupled.
• We aim for loose coupling.
• (A class diagram provides (limited) hints at the
degree of coupling.)
Loose coupling
• We aim for loose coupling.
• Loose coupling increases maintainability.
• No public fields/member variables
• Do not use public fields of other classes
either
• Use accessor and mutator methods
• Within limits!
Tight coupling
• We try to avoid tight coupling.
• A class uses a plethora of methods from
another class
• Relying heavily on constants and variables
defined in another class
• Class doesn’t work in isolation if taken out of
the current project.
Encapsulation
• = hiding information from view.
• Only information about WHAT a unit, e.g.
class, can do should be visible from the
outside.
• The HOW should be hidden/ “encapsulated”.
• Reduces coupling.
Cohesion
• Cohesion refers to the number and diversity of
tasks that a single unit is responsible for.
• If each unit is responsible for one single logical
task, we say it has high cohesion.
• ‘Unit’ applies to classes, methods and modules
(packages).
• We aim for high cohesion.
High cohesion
• We aim for high cohesion.
• High cohesion makes it easier to:
– understand what a class or method does;
– use descriptive names for variables,
methods and classes;
– reuse classes and methods.
– Read other people’s code.
High cohesion
• Class level:
– Classes should represent one single, well
defined entity;
– hold all the information they need
• Method level:
– A method should be responsible for one and
only one well defined task.
Responsibility-driven design (RDD)
• Question: where should we add a new method
(which class)?
• Each class should be responsible for
manipulating its own data.
• The class that owns the data should be
responsible for processing it.
• RDD leads to low coupling.
JUNIT TESTS
Example Test
@Test
public void testMultiply() {
//MyClass is tested
MyClass tester = new MyClass();
Superclass
Superclass of Poodle
Subclass of Animal
Subclass of Dog
Subclass of Animal
Inheritance
Inheritance
Substitution
Supertype
Supertype of Poodle
Subtype of Animal
Subtype of Dog
Subtype of Animal
Object types
Checked at run
time
Static type
Dynamic type
Checked at
compile time
Substitution
Downcasting
Type Casting
Type Casting
Dynamic method lookup
37
ABSTRACT CLASSES AND INTERFACES
Abstract methods
• Abstract methods have abstract in the
signature.
• Abstract methods have no body (without braces,
and followed by a semicolon).
39
Abstract classes
• Abstract classes cannot be instantiated, i.e. no instances
of this type can be created.
• Its purpose is to serve as a superclass for other classes.
• Concrete subclasses complete the implementation.
• A subclass must implement all abstract methods
(otherwise it will be abstract itself)
• Abstract classes may contain abstract methods and
implemented methods (i.e. with a body) at the same
time.
40
Abstract classes
Why Abstract Classes
instanceof
Examples
Examples
List, Map and Set
• Alternative ways to group objects.
• Varying implementations available:
– ArrayList, LinkedList
– HashSet, TreeSet
• Sets do not hold duplicates.
• Maps have unique keys.
• But HashMap is unrelated to HashSet, despite similar
names.
• The second word reveals organisational relatedness.
THAT’S IT!