Exception Handling
Exception Handling
Object-Oriented Programming
Java
The this
Reference
public classMyDate{ private
int day = 26; private
int month = 9; private
int year = 2016;
public MyDate( int day, int
month, int year){
this.day
= day; this.month
= month; this.year
= year;
}
public MyDate( MyDate date){
this.day =
date.day; this.month =
date.month; this.year
= date.year;
}
public MyDate creteNextDate(int moreDays){
MyDate newDate = new MyDate(this);
//... add moreDays
return newDate;
}
}
Java Coding Conventions
● Packages
○ ro.sapientia.ms
● Classes
○ SavingsAccount
● Methods
○ getAmount()
● Variables
○ amount
● Constants
○ NUM_CLIENTS
The final
Keyword
● 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
Relationships between classes
Object-oriented programming
Relationships between classes
● Association
(containment)
– Strong –
Composition
– Weak – Aggregation
Relationships between classes
Composition
– Strong type of
association
– Full ownership
Relationships between classes
Aggregation
– Weak type of
association
– Partial ownership
Relationships between classes
Association – Aggregation - Composition
Associatio
n
Aggregati
on
Compositio
n
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;
public void
enrollStudent( Studen
t student ){
enrolledStudents.add(
student);
}
//...
}
Inheritance, Polymorphism
Outline
● Inheritance
○ Parent class
○ Subclass, Child class
● Polymorphism
○ Overriding methods
○ Overloading methods
○ The instanceof
operator
○ Heterogeneous
collections
Problem: repetition in implementations
public class
Manager{ private String
name; private double
salary; private Date
birthDate; private String
department;
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;
}
//...
}
d
e
p
a
r
t
Access Control
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( ((Ma
nager) 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
Abstraction
Abstraction is achieved in 2
ways :
•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;