Lec-01-OOP-UML-Java
Lec-01-OOP-UML-Java
Lecture 1
OOP and UML Class Diagrams
OOP with Java
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 2
Class Hierarchies
• Superclass and subclass
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 3
Pillars of Object-Oriented Programming
1) Abstraction
– Modelling attributes and behaviors of real objects, in specific
contexts
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 4
Pillars of Object-Oriented Programming (cont.)
2) Encapsulation
– Hiding parts of an object’s
states and behaviors from
others, and exposing a
limited set of interfaces
– public, private, and
protected
– Interfaces and abstract
classes
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 5
Pillars of Object-Oriented Programming (cont.)
3) Inheritance
– Main benefit: code reuse
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 6
Pillars of Object-Oriented Programming (cont.)
4) Polymorphism
– Performing an action in
many forms
– A mechanism for detecting
the real class of an object
and call its implementation
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 7
More Relations Between Objects
• Dependency
• Association
• Aggregation
• Composition
[Figures in this slide are extracted from “Dive Into Design Patterns” by Alexander Shvets] 8
OOP with Java: Declaring Classes and Creating Objects
• Class declaration
class MyClass extends MySuperClass implements YourInterface {
// field, constructor, and
// method declarations
}
• Creating objects
– Declaration, instantiation, initialization
Account a = new Account("Garfield", 8);
10
OOP with Java: Inheritance
• Classes can be derived from other classes, inheriting fields
and methods
• Definitions
– Subclass (derived class/extended class/child class)
– Superclass (base class/parent class)
• Every class has one and only one direct superclass (single
inheritance)
– Excepting Object, which has no superclass
• A subclass inherits all the members (fields, methods, and
nested classes) from its superclass
11
OOP with Java: What You Can Do in a Subclass
• Use the inherited members as is, replace them, hide them, or
supplement them
– Declare a field in the subclass with the same name as the one in the
superclass, thus hiding it (NOT recommended)
– Write a new instance method in the subclass that has the same
signature as the one in the superclass, thus overriding it
– Write a new static method in the subclass that has the same
signature as the one in the superclass, thus hiding it
– Write a subclass constructor that invokes the constructor of the
superclass
• How about private members in a superclass?
12
OOP with Java: Abstract and Final Methods/Classes
14
OOP with Java: Implementing and Using Interfaces
• Include an implements clause in the class declaration
– Your class can implement more than one interface
• If you define a reference variable whose type is an interface,
any object you assign to it must be an instance of a class that
implements the interface
15
OOP with Java: Abstract Classes vs. Interfaces
• Similarities and differences
• Consider using abstract classes when
– You want to share code among several closely related classes
– You expect that classes extending the abstract class have many
common methods or fields, or require access modifiers other than
public
– You want to declare non-static or non-final fields
• Consider using interfaces when
– You expect that unrelated classes would implement your interface
– You want to specify the behavior of a particular data type, but not
concerned about who implements its behavior
– You want to take advantage of multiple inheritance
16