OOP Using Java
A Review
OOP Using Java February 2019 1
Structured Programming
• Back in the "old days" we had Structured Programming:
• Data was separate from code.
• Programmer is responsible for organizing everything in to logical units
of code/data.
• No help from the compiler/language for enforcing modularity, …
Hard to build large systems (not impossible, just hard).
OOP Using Java February 2019 2
OOP To The Rescue
• Model the world as objects.
• Keep data near the relevant code.
• Provide a nice packaging mechanism for related
code.
• Objects can send "messages" to each other.
OOP Using Java February 2019 3
An Object
• Collection of:
• Attributes (object state, data members, instance variables, ..)
• Methods (behaviors, …)
• Each object has it's own memory for maintaining state (the
fields).
• All objects of the same type share code.
OOP Using Java February 2019 4
Modern OOP Benefits
• Code re-use
• programmer efficiency
• Encapsulation
• code quality, ease of maintenance
• Inheritance
• efficiency, extensibility.
• Polymorphism
• power!
OOP Using Java February 2019 5
Code Re-Use
• Nice packaging makes it easy to document/find appropriate
code.
• Everyone uses the same basic method of organizing code (object
types).
• Easy to re-use code instead of writing minor variations of the
same code multiple times (inheritance).
OOP Using Java February 2019 6
Encapsulation
• Information Hiding.
• Don't need to know how some component is
implemented to use it.
• Implementation can change without effecting any calling
code.
OOP Using Java February 2019 7
Inheritance
• On the surface, inheritance is a code re-use issue.
• We can extend code that is already written in a manageable manner.
• Take an existing object type (collection of fields and methods) and extend it.
• create a special version of the code without re-writing any of the existing code (or
even explicitly calling it!).
• End result is a more specific object type, called the sub-class / derived class / child
class.
• The original code is called the superclass / parent class / base class.
OOP Using Java February 2019 8
Inheritance Example
• Employee: name, email, phone
• FulltimeEmployee: also has salary, office, benefits, …
• Manager: CompanyCar, can change salaries, rates contracts, offices, etc.
• Contractor: HourlyRate, ContractDuration, …
• A manager a special kind of FullTimeEmployee, which is a special kind of
Employee.
OOP Using Java February 2019 9
Polymorphism
• Create code that deals with general object types, without the
need to know what specific type each object is.
• Generate a list of employee names:
• all objects derived from Employee have a name field!
• no need to treat managers differently from anyone else.
OOP Using Java February 2019 10
Method Polymorphism
• The real power comes with methods/behaviors.
• A better example:
• shape object types used by a drawing program.
• we want to be able to handle any kind of shape someone wants to code
(in the future).
• we want to be able to write code now that can deal with shape objects
(without knowing what they are!).
OOP Using Java February 2019 11
Java OOP
• Create new object type with class classname {
class keyword. field declarations
• A class definition can contain: { initialization code }
• variables (fields) Constructors
• initialization code
Methods
}
• methods
OOP Using Java February 2019 12
Creating an Object
• Defining a class does not create an object of that class - this
needs to happen explicitly:
classname varname = new classname();
• In general, an object must be created before any methods can be
called.
• the exceptions are static methods.
OOP Using Java February 2019 13
What does it mean to create an object?
• An object is a chunk of memory:
• Holds field values
• Holds an associated object type
• All objects of the same type share code
• they all have same object type, but can have different field values.
OOP Using Java February 2019 14
Inheritance vs. Composition
• When one object type depends on another, the relationship could
be:
• IS-A
• HAS-A
OOP Using Java February 2019 15
Composition
• One class has instance variables that refer to object of another.
• Sometimes we have a collection of objects, the class just provides the glue.
• establishes the relationship between objects.
• There is nothing special happening here (as far as the compiler is
concerned).
OOP Using Java February 2019 16
Inheritance
• One object type is defined as being a special version of some other object
type.
• a specialization.
• The more general class is called:
• base class, super class, parent class.
• The more specific class is called:
• derived class, subclass, child class.
OOP Using Java February 2019 17
Interfaces
• An interface is a definition of method prototypes and possibly some constants
(static final fields).
• An interface does not include the implementation of any methods, it just defines a
set of methods that could be implemented.
•A class can implement an interface, this means that it provides
implementations for all the methods in the interface.
• Java classes can implement any number of interfaces (multiple interface
inheritance).
OOP Using Java February 2019 18