OOP Design Principals 2
OOP Design Principals 2
EECS 3311
Ilir Dema
[email protected]
Outlines
• Overview of Java
• Java Characteristics
• Process of Run Java Code
• Java Class and API
• Primitive Types
• Java Operators
• Access modifiers
• Java Utils
• OOP Design Principals
• Polymorphism
• Inheritance
• Abstraction
• Generics
• Exception
• Junit
• Strategies to write good Junit test cases
Acknowledgement
• Some materials/examples used in this lecture
come from ICSH22@UCI, ECE444@UT,
CS446@UW, CSE331@UW
Polymorphism
• Polymorphism: many (poly) shapes (morph)
Animal
weight : int
Inheritance should create
+ getWeight() : int
an is-a relationship,
meaning the child is a
more specific version of
Bird the parent
+ fly() : void
Deriving Subclasses
Animal
• Inheritance is transitive
• An instance of class Parrot is also an instance of Bird, an instance
of Animal, …, and an instance of class Object
Defining Methods in the Child Class:
Overriding by Replacement
• A child class can override the definition of an inherited method in
favor of its own
• that is, a child can redefine a method that it inherits from its parent
• the new method must have the same signature as the parent's method,
but can have different code in the body
Animal
Car Truck
- carCapacity: int - truckCapacity: int
What Are Abstract Classes Used For?
Abstract classes are used heavily in Design Patterns
Creational Patterns: Abstract class provides interface for creating
objects. The subclasses do the actual object creation.
Structural Patterns: How objects are structured is handled by an abstract
class. What the objects do is handled by the subclasses.
Behavioural Patterns: Behavioural interface is declared in an abstract
superclass. Implementation of the interface is provided by subclasses.
Transaction
- computeValue(): int
RetailSale StockTrade
- computeValue(): int - computeValue(): int
Defining Abstract Methods
Note: no implementation
public abstract class Transaction
{
public abstract int computeValue(); Transaction
- computeValue(): int
Java generics
• lets you write code that is safer and easier to read
• is especially useful for general data structures, such
as ArrayList
// in Java 1.4:
ArrayList names = new ArrayList(); <=> new ArrayList<Object>();
names.add("Marty Stepp");
names.add("Stuart Reges");
String teacher = (String) names.get(0);
• By putting the Type in < >, you are demanding that any client
that constructs your object must supply a type parameter.
• You can require multiple type parameters separated by commas.
• The rest of your class's code can refer to that type by name.
• The convention is to use a 1-letter name such as:
T for Type, E for Element, N for Number, K for Key, or V for Value.
@SuppressWarnings("unchecked")
public Foo(T param) {
myField = param; // ok
T[] a2 = (T[]) (new Object[10]); // ok
}
}
• Example:
public class TreeSet<T extends E> {
...
}
Complex bounded types
• Copy all elements from src to dst. For this to be reasonable, dst
must be able to safely store anything that could be in src. This
means that all elements of src must be of dst's element type or a
subtype.
Wildcards
• ? indicates a wild-card type parameter, one that can be any
type.
• List<?> list = new List<?>(); // anything
Throwable
Error Exception
RunTimeException
LinkageError
ArithmeticException
IndexOutOfBoundsException
ThreadDeath StringIndexOutOfBoundsException
IllegalArguementException
VirtualMachineError NumberFormatException
IllegalAccessException
AWTError NoSuchMethodException
40
ClassNotFoundException
Extending Exception Classes
• A method can throw several exceptions (each of which is a
subclass of the Exception class), e.g.:
public void scaleArray( int[] A, int s) throws ArrayRangeException,
IllegalArgumentException
{…
if (Index >= A.length)
throw new ArrayRangeException( “array too small ” + Index)
…}
https://www.codejava.net/java-core/exception/how-to-create-custom-exceptions-in-java
Examples (cont.)