February 26
Object Oriented Programming (OOP) Introduction
(This class taught by Victor!)
CLASS
A class is like a template. It is the description/definition of an autonomous
"thing".
Classes serve to satisfy the main tenets/principles of OOP.
-- encapsulation
-- abstraction
Classes provide *structure*.
Classes have two kinds of components:
1. attributes: data (nouns)
2. methods: functions (verbs)
These are called "members" of a class.
* * *
class Mammal
{
\\ attributes
...
\\ constructors
Mammal(){}
Mammal(arg1){}
Mammal(arg1, arg2){}
...
\\ methods
..
}
. . . . . . . . . . . . . . . . . . . . . . .
OBJECT
An object is an actual instance of a class. If the class is Human, then
Douglas Repetto would be an object of the class Human. i.e. an instance
of the class. All humans have a head, but no humans share Doug's head.
Only Douglas has his own head!
* * *
Human douglas = new Human(arg1, arg2, ...);
. . . . . . . . . . . . . . . . . . . . . . .
METHOD
Methods are an object's possible actions and abilities to transform its state
(i.e. its attributes) or to do something upon being called with external
information.
douglas.attribute; \\ attribute of object douglas
douglas.method(); \\ method of object douglas
. . . . . . . . . . . . . . . . . . . . . . .
INHERITANCE
"Don't rewrite functionality you already have. Instead, extend it."
Inheritance is an 'is-a' relationship.
‘Subclasses’ are more specialized/detailed versions of a class. They inherit
attributes and methods from their parent classes, and introduce their own.
The inherited members can be extended or entirely redefined by the Subclass.
class Human extends Mammal
{
\\ attributes
...
\\ constructors
Human(){
...
super();
}
Human(arg1){
...
super(arg1);
}
...
\\ methods
..
}
. . . . . . . . . . . . . . . . . . . . . . .
ENCAPSULATION
"Program for an interface, not for an implementation."
Encapsulation hides the implementation (the how to) from the client
(i.e. the one sending messages to the class via methods).
This ensures that the client and the server are kept independent.
What client does does not depend on what the server knows about the client.
. . . . . . . . . . . . . . . . . . . . . . .
ABSTRACTION
"Doug is a Human is a Mammal is a Vertebrate is an Animal."
Abstraction attempts to simplify complex problems by modelling classes
appropriate to the problem level. Inheritance allows us to work at the
appropriate level and to traverse these problem levels.
* * *
Human douglas = new Human();
Mammal m = douglas; \\ this is ok because Human (and thus douglas) is a Mammal.
. . . . . . . . . . . . . . . . . . . . . . .
POLYMORPHISM
"Preserve interface, change functionality."
Polymorphism is the ability of objects belonging to different data types to
respond to method calls of methods of the same name, each one having its own
behavior.
Overriding Polymorphism:
Dancer.dance()
MichaelJackson.dance()
Baryshnikov.dance()
"Both can dance, but they dance differently."
Overloading Polymorphism:
3 + 4 (addition)
'a' + 'b' (concatenation)
.........................................................