Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)"— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived) classes are created by absorbing the methods and variables of an existing (base) class –It then adds its own methods to enhance its capabilities –“Is a” relationship: derived class object can be treated as base class object –“Has a” relationship: class object has object references as members –A derived class can only access non-private base class members unless it inherits accessor functions –Constructors are not inherited

2  2002 Prentice Hall. All rights reserved. 2 Base Classes and Derived Classes To specify that class Two is derived from class One class Two extends One or class Circle extends Shape { {

3  2002 Prentice Hall. All rights reserved. 3 Members All inheritance in Java is public inheritance –Can be accessed by base class or any class derived from that base class –No analog to C++ features of private and protected Definitions –super, base, parent class –child, sub-, derived class

4  2002 Prentice Hall. All rights reserved. 4 Base Classes and Derived Classes Inheritance forms a tree-like hierarchy Inheritance hierarchy for university CommunityMember s. CommunityMember EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

5  2002 Prentice Hall. All rights reserved. 5 Relationship between Base Classes and Derived Classes Text’s Example: –class Employee (parent class) –class Manager (child class)

6  2002 Prentice Hall. All rights reserved. Outline 6 class Employee { public Employee(String n, double s, int year, int month, int day) { … } public String getName() { … } public double getSalary() { … } public Date getHireDay() { … } public void raiseSalary(double byPercent) { … } private String name; private double salary; private Date hireDay; }

7  2002 Prentice Hall. All rights reserved. Outline 7 class Manager extends Employee { //added methods here //redefined methods here private double bonus; } public double getSalary() { return salary + bonus; //won’t work } public double getSalary() { double baseSalary = getSalary(); //still won’t work return baseSalary + bonus; } Manager class has no direct access to private fields of superclass Calls itself – infinite recursion

8  2002 Prentice Hall. All rights reserved. Outline 8 class Manager extends Employee { /** @param n the employee's name @param s the salary @param year the hire year @param month the hire month @param day the hire day */ public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; } public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double b) { bonus = b; } private double bonus; } Manager inherits from Employee Calls the constructor for the employee class Calls the getSalary method for the employee class

9  2002 Prentice Hall. All rights reserved. Outline 9 // construct a Manager object Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); boss.setBonus(5000); Employee[] staff = new Employee[3]; // fill the staff array with Manager and Employee objects staff[0] = boss; staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15); // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); Create a new manager and set bonus Create an array of parent class Employee Fill array with employees and manager foreach loopWhich getSalary( ) will it call?

10  2002 Prentice Hall. All rights reserved. Outline 10 Carl’s salary is 80000 + 5000 = 85000

11  2002 Prentice Hall. All rights reserved. 11 Polymorphism The fact that an object variable (such as e) can refer to multiple actual types is called polymorphism. Automatically selecting the appropriate methods at run time is called dynamic binding. Note: In Java, you do not need to declare a method as virtual. Dynamic binding is the default behavior. If you do not want a method to be virtual, you tag it as final.

12  2002 Prentice Hall. All rights reserved. 12 Preventing Inheritance: Final Methods class Employee { … public final String getName( ) { return name; } … }

13  2002 Prentice Hall. All rights reserved. 13 Inheritance Inheritance does not have to stop at one layer of classes. Could have an Executive class that extends Manager. Path from a particular class to its ancestor is the inheritance chain. May be more than one chain of descent. For example, a subclass Programmer and Secretary that extends Employee. Java does not support multiple inheritance. (Has interface classes, though.)

14  2002 Prentice Hall. All rights reserved. 14 Polymorphism A simple rule for inheritance ‘is-a’ rule. –subclass ‘is-an’ object of the superclass –every manager ‘is-an’ employee The opposite is not true. Another way of formulating rule is substitution principle. –You can use a subclass object whenever the program expects a superclass object. –Already did this in last example.

15  2002 Prentice Hall. All rights reserved. 15 Polymorphism Last example. –staff[0] and boss refer to the same object. –staff[0 ] is only considered to be an Employee object by compiler. –That means you can call boss.setBonus (5000); //ok –but you can’t call staff[0].setBonus(5000); //error

16  2002 Prentice Hall. All rights reserved. 16 Preventing Inheritance: Final Classes final class Executive extends Manager { … }


Download ppt " 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)"

Similar presentations


Ads by Google