Professional Documents
Culture Documents
Exception Handling
Exception Handling
Exception Handling
Object-Oriented Programming
Java
The this Reference
● Class
○ You cannot subclass a final class.
● Method
○ You cannot override a final method.
● Variable
○ A final variable is a constant.
○ You can set a final variable only once.
○ Assignment can occur independently of
the declaration (blank final variable).
Relationships between classes
Object-oriented programming
Relationships between classes
● Association (containment)
– Strong – Composition
– Weak – Aggregation
Relationships between classes
Composition
Association
Aggregation
Composition
Relationships between classes
Implementing Associations (1)
– One-to-one
– One-to-many
Relationships between classes
Implementing one-to-many relationship (1)
public class Student{
private final long ID;
private String firstname;
private String lastname;
//...
}
//...
}
Relationships between classes
Implementing one-to-many relationship (2)
public class Course{
private final long ID;
private String name;
public static final int MAX_STUDENTS = 100;
private Student[] enrolledStudents;
private int numStudents;
//...
}
Inheritance, Polymorphism
Outline
● Inheritance
○ Parent class
○ Subclass, Child class
● Polymorphism
○ Overriding methods
○ Overloading methods
○ The instanceof operator
○ Heterogeneous collections
Problem: repetition in implementations
public Manager( … ){
// …
}
public String toString(){
// …
}
}
Inheritance - syntax
}
The subclass
● Opportunities:
1) add new data → department
2) add new methods → e.g. getDepartment()
3) override inherited methods → toString()
Invoking Parent Class Constructors
public class Employee{
protected String name;
protected double salary;
protected Date birthDate;
public Employee( String name, double salary, Date birthDate){
this.name = name;
this.salary = salary;
this.birthDate = birthDate;
}
//...
}
private Yes
default! Yes Yes
protected Yes Yes Yes
public Yes Yes Yes Yes
Polymorphism - Overriding Methods
// print employees
for( Employee e: emps ){
System.out.println( e.toString() );
}
// count managers
int counter = 0;
for( Employee e: emps ){
if( e instanceof Manager ){
++counter;
}
}
Static vs. Dynamic type of a reference
//Solution
System.out.println( ((Manager) e).getDepartment() );// CORRECT
//Better Solution
if( e instanceof Manager ){
System.out.println( ((Manager) e).getDepartment() );
}
The instanceof Operator
//expressions
a instanceof Animal → true
a instanceof Mammal → true
a instanceof Bear → true
a instanceof Date → false
Polymorphism
Overloading Methods
● Inheritance
– Subclass opportunities
● Polymorphism
– Overriding methods
– Overloading methods
– Polymorphic argument
– Heterogeneous collections
– Static vs. dynamic type
– The instanceof operator
Abstraction
•Abstract class
}
final void eat(){
System.out.println("All Animal Eat");
}
}
Interfaces
interface Animal{
void run();
// void eat();
}
Static can be :
1.Variable (also known as a class variable)
2.Method (also known as a class method)
class School{
public static String name;