Chapter 2.
Object-Oriented
Design
INFORMATION TECHNOLOGY PROGRAMING IN JAVA
1 IUH – FIT – 2024
Chapter 2. Object-Oriented Design
1. Goals, Principles, and Patterns
2. Inheritance
3. Interfaces and Abstract Classes
4. Exceptions
5. Casting and Generics
6. Nested Classes
2
2.1 Goals, Principles, and Patterns
2.1.1 Object-Oriented Design Goals
• Executable statements are placed in functions, known as methods, that belong
to class definitions.
• Each object is an instance of a class.
• Software implementations should achieve robustness, adaptability, and
reusability.
• Robustness: A program produces the right output for all the anticipated inputs.
• Adaptability: to be able to evolve over time in response to changing conditions
in its environment.
• Reusability: the same code should be usable as a component of different
systems in various applications.
3
2.1 Goals, Principles, and Patterns
2.1.2 Object-Oriented Design Principles
• Abstraction
• Encapsulation
• Modularity
4
2.1 Goals, Principles, and Patterns
2.1.2 Object-Oriented Design Principles
• The notion of abstraction is to distill a complicated system down to its most
fundamental parts.
• The design of data structures gives abstract data types (ADTs).
• An ADT specifies what each operation does, but not how it does it.
• In Java, an ADT can be expressed by an interface.
• Interface is simply a list of method declarations, where each method has an
empty body.
5
2.1 Goals, Principles, and Patterns
2.1.2 Object-Oriented Design Principles
• Encapsulation gives one programmer freedom to implement the details of a
component.
• Encapsulation yields robustness and adaptability.
• Encapsulation allows the implementation details of parts of a program to
change without adversely affecting other parts.
• It is easier to fix bugs or add new functionality with relatively local changes to a
component.
6
2.1 Goals, Principles, and Patterns
2.1.2 Object-Oriented Design Principles
• Modularity refers to an organizing principle in which different components of a
software system are divided into separate functional units.
• It is easier to test and debug separate components before integrating them into a
larger software system.
7
2.1 Goals, Principles, and Patterns
2.1.3 Design Patterns
• Object-oriented design facilitates reusable, robust, and adaptable software.
• Design patterns fall into two groups:
• patterns for solving algorithm design problems;
• patterns for solving software engineering problems.
8
2.1 Goals, Principles, and Patterns
2.1.3 Design Patterns
• Some of the algorithm design patterns we discuss include the following:
• Recursion (Chapter 5)
• Amortization (Sections 7.2.3, 11.4.4, and 14.7.3)
• Divide-and-conquer (Section 12.1.1)
• Prune-and-search, also known as decrease-and-conquer (Section 12.5.1)
• Brute force (Section 13.2.1)
• The greedy method (Sections 13.4.2, 14.6.2, and 14.7)
• Dynamic programming (Section 13.5)
9
2.1 Goals, Principles, and Patterns
2.1.3 Design Patterns
• Some of the software engineering design patterns:
• Template method (Sections 2.3.3, 10.5.1, and 11.2.1)
• Composition (Sections 2.5.2, 2.6, and 9.2.1)
• Adapter (Section 6.1.3)
• Position (Sections 7.3, 8.1.2, and 14.7.3)
• Iterator (Section 7.4)
• Factory Method (Sections 8.3.1 and 11.2.1)
• Comparator (Sections 9.2.2, 10.3, and Chapter 12)
• Locator (Section 9.5.1)
10
2.2 Inheritance
2.2.1 Definition
• A hierarchical design is useful in software development, as common
functionality can be grouped at the most general level.
• The mechanism for a modular and hierarchical organization is a technique
known as inheritance.
• A new class to be defined based upon an existing class.
• Existing class: the base class, parent class, or superclass.
• The newly defined class is known as the subclass or child class.
• The subclass extends the superclass.
11
2.2 Inheritance
2.2.1 Definition
• The subclass automatically inherits, all methods from the superclass.
• The subclass can differentiate itself from its superclass in two ways:
• It may augment the superclass by adding new fields and new methods.
• It may also specialize existing behaviors by providing a new implementation that
overrides an existing method.
12
2.2 Inheritance
2.2.2 Extending the CreditCard Class
• A UML diagram showing PredatoryCreditCard as a subclass of CreditCard.
13
2.2 Inheritance
2.2.2 Extending the CreditCard Class
14
2.3 Interfaces and Abstract Classes
2.3.1 Interfaces in Java
• An interface is a collection of method declarations with no data and no bodies.
• The methods of an interface are always empty.
• Interfaces do not have constructors, and they cannot be directly instantiated.
• When a class implements an interface, it must implement all of the methods
declared in the interface.
15
2.3 Interfaces and Abstract Classes
2.3.1 Interfaces in Java
SuperClass SubClass
16
2.3 Interfaces and Abstract Classes
2.3.2 Multiple Inheritance for Interfaces
• The ability of extending from more than one type is known as multiple
inheritance.
• In Java, multiple inheritance is allowed for interfaces but not for classes.
17
2.3 Interfaces and Abstract Classes
2.3.2 Multiple Inheritance for Interfaces
18
2.3 Interfaces and Abstract Classes
2.3.3 Abstract Classes
• In Java, an abstract class serves a role somewhat between that of a traditional
class and that of an interface.
• An abstract class may define one or more fields and any number of methods
with implementation (concrete methods).
• An abstract class may also extend another class and be extended by further
subclasses.
19
2.3 Interfaces and Abstract Classes
2.3.3 Abstract Classes
20
2.4 Exceptions
2.4.
• Exceptions are unexpected events that occur during the execution of a program.
• an unavailable resource
• unexpected input from a user
• simply a logical error on the part of the programmer
• In Java, exceptions are objects that can be thrown by code.
• An exception may also be caught by a surrounding block of code.
• If an exception occurs and is not handled, then the Java runtime system will
terminate the program.
21
2.4 Exceptions
2.4.1 Catching Exceptions
• The Java runtime environment begins performing a try-catch statement.
• A typical syntax for a try-catch statement in Java is as follows:
• guardedBody: the block of statements
• exceptionTypei is the type of some exception
• variablei is a valid Java variable name
22 [Link]
2.4 Exceptions
2.4.1 Catching Exceptions
• A demonstration of catching an exception:
23
2.4 Exceptions
2.4.2 Throwing Exceptions
• A throw statement is typically written as follows:
• The Throws Clause: the syntax for declaring possible exceptions in a method
signature relies on the keyword throws.
24
2.4 Exceptions
2.4.3 Java’s Exception Hierarchy
• A typical syntax for a try-catch statement in Java is as follows:
• All subtypes of RuntimeException in Java are officially treated as unchecked
exceptions.
• Any exception type that is not part of the RuntimeException is a checked exception.
• In particular, all checked exceptions that might propagate upward from a
method must be explicitly declared in its signature.
25
2.5 Casting and Generics
• See Chapter 2, page 88.
26
2.6 Nested Classes
• Java allows a class definition to be nested inside the definition of another class.
• The containing class is known as the outer class.
• The nested class is formally a member of the outer class, and its fully qualified
name is [Link].
27