Download presentation
Presentation is loading. Please wait.
Published byLeon Taylor Modified over 9 years ago
1
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/14) Object Oriented Programming Joel Adams and Jeremy Frens Calvin College
2
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/14) Review: Inheritance We’ve seen that classes can be organized into hierarchies… Designing your system to take maximum advantage of inheritance is what distinguishes OOP from traditional programming. Person Employee Salaried Employee Hourly Employee ManagerEngineerInternTemp
3
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/14) Inheritance and Design Inheritance reflects the is-a relationship, and can be properly applied to any X that is a more specialized kind of Y: Rock IgneousMetamorphicSedimentary Sand stone Lime stone … MarbleSlate … BasaltGranite … Object Component Container JComponent AbstractButton JButton
4
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/14) How do we know when an X is-a Y? Design Issue: Is-A 1. If the relationship holds in the real world, it probably holds in software, too. 2. If every message that can be sent to a Y can be correctly sent to an X, then X is-a Y: X Y Snake AspBoaRattler … Queue Dequeue Stack ?
5
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/14) Design Issue: Avoiding Ifs You might be tempted to write code like this: public class Bird { private String myKind = null; public Bird(String kind) { myKind = kind; } public String getCall() { if (myKind.equals("Parrot")) return "squawk"; else if (myKind.equals("Chicken")) return "cluck";... else if (myKind.equals("Duck")) return "quack"; else return "cheep"; // default call } Adding support for a new kind of bird requires us to modify this class.
6
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/14) public class Chicken extends Bird { public Chicken() { super("Chicken"); } public String getCall() { return "cluck"; } } public class Parrot extends Bird { public Parrot() { super("Parrot"); } public String getCall() { return "squawk";} } public class Bird { private String myKind = null; public Bird(String kind) { myKind = kind; } public String getKind() { return myKind; } public String getCall() { return "cheep"; } } Design Issue: Avoiding Ifs (ii) Instead, OOP lets us write it like this: Now, adding a new kind of bird requires no existing code to be modified.
7
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/14) In that example, subclasses override a method they inherit from their superclass: Overriding Bird [] birdArray = { new Parrot(), new Chicken(),... new Duck() }; We can then write: Bird getCall() Chicken getCall() Duck getCall() Parrot getCall() … for (int i = 0; i < birdArray.length; i++) { Bird aBird = birdArray[i]; System.out.println( aBird.getKind() // inherited + ": " + aBird.getCall() ); // overridden }
8
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/14) To what definition do we bind aBird.getCall () ? Polymorphism Pretend we are compilers for a moment… How do we resolve the invocation: aBird.getCall() ? Java uses run-time binding by default ( virtual in C++). Runtime binding is also known as polymorphism. The type of handle aBird is Bird … But if we bind aBird.getCall() to Bird#getCall(), then we will not get the call of a given bird. What aBird is pointing to is unknown at compile-time. This information remains unknown until run-time…
9
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/14) Issue: When to use Polymorphism? Polymorphism is best used when “peer” subclasses need to have different behaviors in response to the same message: public class SalariedEmployee extends Employee {... public double getPay() { return mySalary; } } public class HourlyEmployee extends Employee {... public double getPay() { return myHours * myWage; } } The behavior is usually more complicated than this…
10
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/14) If all subclasses of a superclass should accept a message, but the superclass cannot define that message, it can defer its definition to the subclasses by declaring an abstract method: Abstract Methods abstract public class Employee extends Person {... abstract public double getPay(); // all subclasses } // must define this public class SalariedEmployee extends Employee {... public double getPay() { return mySalary; } } public class HourlyEmployee extends Employee {... public double getPay() { return myHours * myWage; } }
11
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(11/14) Review: Super … lets you invoke a method that you’ve overridden: public class Person {... public String toString() { return myName; } } public class Employee extends Person {... public String toString() { return super.toString() + " (" + myID + ")"; } } public class SalariedEmployee extends Employee {... public String toString() { return super.toString() + "\n" + mySalary; } }
12
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(12/14) Java supports three access modes for variables and methods: Restricting Access private - can only be accessed within this class protected - can be accessed within this class or a subclass public - can be accessed from any class Rules of Thumb: 1.Make instance variables and non-interface methods private, to prevent “outsiders” from accessing them. 2.Make mutators protected, so that subclasses (but no one else) can modify instance variables if necessary. 3.Make other methods (accessors, constructors, …) public
13
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(13/14) Summary Inheritance lets you minimize redundant coding by consolidating such code within superclasses, and inheriting it from there. Inheritance is best used when every message that can be sent to a superclass can correctly be sent to a subclass. Overriding lets a subclass redefine an inherited method with a definition appropriate for that subclass. Polymorphism (aka runtime binding) ensures that a message invokes the method definition in the message’s receiver. Abstract methods (aka C++ pure virtual functions) are superclass methods that a non-abstract subclass must define. The compiler won’t allow you to create an instance of an abstract class.
14
2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(14/14) Exercise Spend the remainder of today’s session working in pairs on today’s exercise, which puts today’s ideas into practice.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.