2. About me
• Joined IGN in Aug 2009 to build the Social API
Platform
• Worked at E*Trade, Accenture, WaMu, BEA
• Started working with Java ecosystem in ’96
• @lobster1234 on Twitter, Slideshare, Github and
StackOverflow
4. Procedural Code
#include<stdio.h>
int main(){
int what, result;
printf("nFactorial of what, fellow code-foo’er? t");
scanf("%d",&what);
result = function(what);
printf("nThe number you are looking for is %d n", result);
}
int function(int number){
int result;
if(number==1) return 1;
else result = number*function(number -1);
return result;
}
5. Lets analyze..
• Simple to write, as it is linear. Think machine.
• Can kick any OO language’s ass in performance
BUT
• No separation of data and behavior
• No concept of visibility of variables
• No encapsulation
• Hard to map real world problems as humans – great for
algorithms though.
• Global, shared data.
• Reusability achieved via copy paste, header files (.h) or
shared object files (.so files in Unix)
6. Objects – What?
• Abstraction over linear programming
• Keywords – reduce, reuse, recycle
• Modeling a problem around data and
behavior vs. a big block of code
• Identify patterns to model
– Classes
– Methods
• HUMAN!
7. Confused?
• Let me confuse you more with the cliché,
textbook examples of
– Shapes (Circle, Triangle, Rectangle, Square)
– Animals (Dog, Cat, Pig, Tiger)
– Vehicles (Car, Truck, Bike)
8. We can do better!
• Lets model IGN the OO-way
– We have games
– We have properties of games
– We have users (who have properties too)
– Users follow games
– Users follow other users
– Users rate games
– Users update status
9. Real world enough for you?
• Actors – Nouns – Users, Games, Activities,
Status
• Actions – Verbs – Rate, Follow, Update
• Properties
– User – nickname, age, gender
– Game – publisher, title, description
10. Connect the dots
• Actors, Nouns – are Classes
• Actions, Verbs – are functions or methods
• A function operates on data encapsulated within
a Class
• Every actor has certain properties
• A caller of this Class doesn’t give a shit to the
implementation details (abstraction, data hiding)
• Collectively, the properties and methods in a class
are called its members.
11. Going back to C
#include<stdio.h>
int main(){
char* title; char* publisher;
// save??
// describe??
}
12. Java
public class Game {
private String title;
private String publisher;
public String getTitle(){
return "Title is " + title;
}
public void setTitle(String title){
if(title!=null)
this.title = title;
else this.title = "";
}
public String getPublisher(){
return "Publisher is " + publisher;
}
public void setPublisher(String publisher){
this.publisher = publisher;
}
public void describe(){
System.out.println(“Title is “ + title + “ Publisher is “ +
publisher);
}
}
13. Principles so far..
• Objects are instances of classes and are created
by invoking the constructor which is defined in a
class.
• Objects have a state and identity, classes don’t.
• Some classes cannot be instantiated as objects
– Abstract classes
– Factories (getting a little ahead of ourselves here!)
• Properties are defined by “has-a” clause
• Classes are defined by “is-a” clause (more on this
later)
14. Inheritance
• Classes can extend other classes and interfaces
– is-a relationship
• A Game is a MediaObject, so is a DVD
• A User is a Person, so is an Editor
• Java does not support multiple inheritance per-
se, but you can mimic that via interfaces.
• A cluster of parent-child nodes is called a
hierarchy
• CS Students – Generalization vs. Specialization
15. Inheritance and Behavior
• Child classes can
– override behavior
– overload behavior
Provided the Parent has provided visibility to its
members
16. Association: Aggregation
• A game has a title
• A game has a publisher
– A Publisher can live without this game. He can
have other games. This is aggregation.
18. Behavior : Interfaces
• An interface defines a behavior of the classes
that inherit it – as seen by the consumers
• Interfaces are actions on verbs – like
Observable, Serializable, Mutable,
Comparable. They add behavior.
• Interfaces do not define implementation
(that’s OO Power!). They just define behavior.
The implementation is encapsulated within
the class. This is polymorphism.
19. Behavior of saving data
• Interface:
public interface Persistable{
public void save(Object o);
}
• Implementations:
public class MySQLStore implements Persistable{
public void save(Object o){
//save in MySQL
}
}
public class MongoStore implements Persistable{
public void save(Object o){
//save in MongoDB
}
}
21. Visibility and Modifiers
• public/protected/package/private
– Scala has explicit package-private
– Accessors and Mutators
• Advanced for this session
– final
– static
– constructors (default, noargs..)
– super
22. Recap : Object Modeling
• Nouns, Verbs
• is-a, has-a
• Inheritance (is-a)
• Association (has-a)
– Composition
– Aggregation
• Data vs. Behavior