Professional Documents
Culture Documents
Lecture-1
Lecture-2
Lecture-3
Week-3
Structured programming
The algorithms come first
Lecture-1
Design a set of procedures for specific tasks
Lecture-2
Combine them to build complex systems
Lecture-3
Data representation comes later
Lecture-4
Design data structures to suit procedural manipulations
Lecture-5
Week-3
Week-3
public class Employee{
A subclass extends a parent class private String name;
private double salary;
Lecture-1
Subclass inherits instance variables and
Lecture-2
methods from the parent class // Some Constructors ...
Week-3
public class Employee{
private String name;
private double salary;
Lecture-1
Manager can redefine bonus()
double bonus(float percent){ // Some Constructors ...
Lecture-2
Week-3
What about e.bonus(p)? Which bonus() public class Employee{
private String name;
do we use? private double salary;
Lecture-1 Static: Use Employee.bonus()
// Some Constructors ...
Lecture-2 Dynamic: Use Manager.bonus()
Lecture-3 // "mutator" methods
Lecture-4
Dynamic dispatch (dynamic binding, public boolean setName(String s){ ... }
Lecture-5
late method binding, . . . ) turns out to public boolean setSalary(double x){ ... }
Lecture-6
be more useful // "accessor" methods
Polymorphism public String getName(){ ... }
public double getSalary(){ ... }
Every Employee in emparray ”knows”
how to calculate its bonus correctly! // other methods
public double bonus(float percent){
Employee[] emparray = new Employee[2]; return (percent/100.0)*salary;
Employee e = new Employee(...); }
Manager e = new Manager(...); }
emparray[0] = e; public class Manager extends Employee{
emparray[1] = m; private String secretary;
for (i = 0; i < emparray.length; i++){ public boolean setSecretary(name s){ ... }
System.out.println(emparray[i].bonus(5.0); public String getSecretary(){ ... }
} }
W03:L03: Dynamic dispatch and polymorphism (Cont.)
Week-3
Signature of a function is its name and double[] darr = new double[100];
the list of argument types int[] iarr = new int[500];
...
Lecture-1
Overloading: multiple methods, different Arrays.sort(darr);
Lecture-2 // sorts contents of darr
signatures, choice is static Arrays.sort(iarr);
Lecture-3
// sorts contents of iarr
Lecture-4 Overriding: multiple methods, same class Arrays{
Lecture-5 signature, choice is static ...
public static void sort(double[] a){..}
Lecture-6 Employee.bonus() // sorts arrays of double[]
Manager.bonus() public static void sort(int[] a){..}
// sorts arrays of int[]
Dynamic dispatch: multiple methods, ...
same signature, choice made at run-time }
W03:L03: Dynamic dispatch and polymorphism (Cont.)
Week-3
public class Employee{
Type casting private String name;
private double salary;
Lecture-1 Consider the following assignment
// Some Constructors ...
Lecture-2
Employee e = new Manager(...)
Lecture-3 // "mutator" methods
Lecture-4
e.setSecretary() does not work public boolean setName(String s){ ... }
Lecture-5
Static type-checking disallows this public boolean setSalary(double x){ ... }
Week-3
Week-3
Week-3