You are on page 1of 11

Week-3

Lecture-1

Lecture-2

Lecture-3

Lecture-4 Programming Concepts Using Java


Lecture-5
Week 3 Revision
Lecture-6
W03:L01: The philosophy of OO programming

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

Lecture-6 Object Oriented design


First identify the data we want to maintain and manipulate
Then identify algorithms to operate on the data
Designing objects
Behaviour – what methods do we need to operate on objects?
State – how does the object react when methods are invoked?
State is the information in the instance variables
Encapsulation – should not change unless a method operates on it
W03:L01: The philosophy of OO programming (Cont.)

Week-3

Relationship between classes


Dependence
Lecture-1
Order needs Account to check credit status
Lecture-2
Item does not depend on Account
Lecture-3
Robust design minimizes dependencies, or coupling between classes
Lecture-4
Aggregation
Lecture-5
Order contains Item objects
Lecture-6
Inheritance
One object is a specialized versions of another
ExpressOrder inherits from Order
Extra methods to compute shipping charges, priority handling
W03:L02: Subclasses and inheritance

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 ...

Lecture-3 Subclass can add more instance variables // "mutator" methods


Lecture-4 and methods public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
Lecture-5 Can also override methods
Lecture-6 // "accessor" methods
Subclasses cannot see private public String getName(){ ... }
components of parent class public double getSalary(){ ... }

Use super to access constructor of // other methods


public double bonus(float percent){
parent class return (percent/100.0)*salary;
}
Manager objects inherit other fields and }
methods from Employee public class Manager extends Employee{
private String secretary;
Every Manager has a name, salary and public boolean setSecretary(name s){ ... }
methods to access and manipulate these. public String getSecretary(){ ... }
}
W03:L03: Dynamic dispatch and polymorphism

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

Lecture-3 return 1.5*super.bonus(percent); // "mutator" methods


Lecture-4 } public boolean setName(String s){ ... }
public boolean setSalary(double x){ ... }
Lecture-5
Uses parent class bonus() via super
Lecture-6 // "accessor" methods
Overrides definition in parent class public String getName(){ ... }
public double getSalary(){ ... }
Consider the following assignment
// other methods
Employee e = new Manager(...) public double bonus(float percent){
Can we invoke e.setSecretary()? return (percent/100.0)*salary;
}
e is declared to be an Employee }
Static typechecking – e can only refer public class Manager extends Employee{
private String secretary;
to methods in Employee public boolean setSecretary(name s){ ... }
public String getSecretary(){ ... }
}
W03:L03: Dynamic dispatch and polymorphism (Cont.)

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){ ... }

Lecture-6 Type casting — convert e to Manager // "accessor" methods


public String getName(){ ... }
((Manager) e).setSecretary(s) public double getSalary(){ ... }

Cast fails (error at run time) if e is not a // other methods


Manager public double bonus(float percent){
return (percent/100.0)*salary;
Can test if e is a Manager }
}
if (e instanceof Manager){ public class Manager extends Employee{
private String secretary;
((Manager) e).setSecretary(s); public boolean setSecretary(name s){ ... }
} public String getSecretary(){ ... }
}
W03:L04: The Java class hierarchy

Week-3

Java does not allow multiple inheritance


Lecture-1
A subclass can extend only one parent class
Lecture-2 The Java class hierarchy forms a tree
Lecture-3
The root of the hierarchy is a built-in class called Object
Lecture-4
Object defines default functions like equals() and toString()
Lecture-5
These are implicitly inherited by any class that we write
Lecture-6
When we override functions, we should be careful to check the signature
Useful methods defined in Object
public boolean equals(Object o) // defaults to pointer equality
public String toString() // converts the values of the
// instance variables to String
For Java objects x and y, x == y invokes x.equals(y)
To print o, use System.out.println(o+””);
Implicitly invokes o.toString()
W03:L05: Subtyping vs inheritance

Week-3

Class hierarchy provides both subtyping and inheritance


Lecture-1 Subtyping
Lecture-2 Capabilities of the subtype are a superset of the main type
Lecture-3 If B is a subtype of A, wherever we require an object of type A, we can use an object of
Lecture-4 type B
Lecture-5 Employee e = new Manager(...); is legal
Lecture-6 Compatibility of interfaces
Inheritance
Subtype can reuse code of the main type
B inherits from A if some functions for B are written in terms of functions of A
Manager.bonus() uses Employee.bonus()
Reuse of implementations
Using one idea (hierarchy of classes) to implement both concepts blurs the distinction
between the two
W03:L06: Java modifiers

Week-3

private and public are natural artefacts of encapsulation


Lecture-1
Usually, instance variables are private and methods are public
Lecture-2
However, private methods also make sense
Lecture-3 Modifiers static and final are orthogonal to public/private
Lecture-4
Use private static instance variables to maintain bookkeeping information across
Lecture-5
objects in a class
Lecture-6
Global serial number, count number of objects created, profile method invocations, . . .
Usually final is used with instance variables to denote constants
A final method cannot be overridden by a subclass
A final class cannot be inherited
Can also have private classes

You might also like