Presentation is loading. Please wait.

Presentation is loading. Please wait.

January 28 Objects Again Inheritance What About Types? Casting.

Similar presentations


Presentation on theme: "January 28 Objects Again Inheritance What About Types? Casting."— Presentation transcript:

1 January 28 Objects Again Inheritance What About Types? Casting

2 Let’s Formally Define Object Oriented Approach zEverything is an object. Should be obvious by now!!! zA program is a bunch of objects telling each other what to do by sending messages. Message sending = Method calling zEach object has its own memory made up of other objects. zEach object has a type. zAll objects of a particular type can receive the same message.

3 Inheritance zAnother way of reusing existent Class. zWhat you acquire from previous generations. zGoing opposite direction from Abstraction. Extending/subclassing a Class to make a more specific(different) Class. zExtended(subclass) can behave as its parent class(super class) zOf course, it can behave as a totally different class

4 Inheritance Hierarchy Employee ManagerSecretaryProgrammer Executive

5 class Employee { private String name; private double salary; private Day hireDay; Employee(String n, double s, Day d) { name =n; salary = s, hireDay = d; } public void print() { System.out.println(name + “ “ + salary + “ “ + hireYear()); } public void raiseSalary(double p) { salary *= 1 + p/100; } public int hireYear() { return hireDay.getYear(); } class Manager extends Employee { private String secretaryName; Manager(String n, double s, Day d) { super(n,s,d); } public void raiseSalary(double p) { super.raiseSalary(p+25); } public void setSecretary(String n) { secretaryName = n; }

6 What’s happening when an object is instantiated? zAll of its super classes are instantiated first. zIf the direct super class no no-arg constructor, we must construct super class using super(arglist) zsuper keyword is also used to point direct super class. zEx) super.raiseSalary(percentage); zThe origin of Java class Hierarchy: Object zEvery class is subclass of Object.

7 What about type? (Who am I?) zRecall that objects A and B are of the same type, If they can receive the same message. zManager class extends Employee and Executive extends Manager. Is Manager the same type of Employee? Is Executive the same type of Employee? Is Employee the same type of Manager? zSubclasses are the type of their super classes. The opposite is not true!!!

8 Casting zUsed to change type of an object, just like forced conversion between primitive data types. zWorks only within an inheritance hierarchy. Employ employ[]; // Some codes are here… Manager boss = (Manager)employ[3]; This is legal. However is this really OK? zThe bottom line is employ[3] should be Manager class in the first place. zinstanceof keyword

9 class EmployeeBase{ staic Employee[] workers = new Employee[1000]; public staic void changeEmployee(int rank, Employee newone) { workers[rank] = newone; } public static Employee getEmployee(int rank) { return workers[rank]; } class EmployeeTest{ public static void main(String args[]) { Manager man = new Manager(“hoony”,”2M”,”1/28/00”); EmployeeBase.changeEmployee(1,man); man = (Manager)EmployeeBase.getEmployee(1); 1. man.setSecretaryName(“Liz”); // May or May not work 2. If ( man instanceof Manager ) man.setSecretaryName(“Liz”); ………………... }


Download ppt "January 28 Objects Again Inheritance What About Types? Casting."

Similar presentations


Ads by Google