Unformatted text preview:

PolymorphismPolymorphismThe Object ClassOverriding MethodsThe equals methodUsing Object.equals methodOur Own equals MethodUsing your equals methodSomething StrangerCompile-time V.S. Run-timeMethod DispatchExample: BankAccountExample: BankAccountExample: Function ArgumentsAdvantages Of Using General TypesMad Libs1PolymorphismA deeper look into Java’s programming modelRobert ToscanoMIT 6.092 IAP 2006 2Polymorphismz Ability of objects belonging to different types to respond to methods of the same namez Ability to override functionality from extended super classz Java handles which overridden versions of methods are to be executedz Lets have a look at some examplesMIT 6.092 IAP 2006 3The Object Classz Every root class, that is a class that does not extend another class, implicitly extends the java.lang.Object classz java.lang.Object contains methods that all classes inheritz These include:z clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, and waitMIT 6.092 IAP 2006 4Overriding Methodsz Superclassz If class A extends class B, then class B is the superclass of Az Consequently, class A is a subclass of class Bz If class B contains a method with the signature:z public void foo (int arg)z Then class A can override the method by providing a method with the same signatureDifferent than Method OverloadingMIT 6.092 IAP 2006 5The equals methodz public boolean equals (Object o);z All classes inherit this method from the Object classz Performs reference equality (checks whether two references refer to the same object in memory)z You must override this method if your class needs to have an idea of equality among instancesMIT 6.092 IAP 2006 6Using Object.equals methodz Two CheckingAccounts are equal if they have the same account balancez CheckingAccount c1 = newCheckingAccount(100);CheckingAccount c2 = newCheckingAccount(100);z c1.equals(c1); z c2.equals(c2);z c1.equals(c2);//== true//== true//== falseMIT 6.092 IAP 2006 7Our Own equals Methodpublic class CheckingAccount extends BankAccount {…public boolean equals (Object o) {if (o instanceof CheckingAccount) {CheckingAccount c = (CheckingAccount)o;return balance == c.balance;} else {return false;}}…}MIT 6.092 IAP 2006 8Using your equals methodz CheckingAccount c1 = newCheckingAccount(100);CheckingAccount c2 = newCheckingAccount(100);z c1.equals(c1);z c2.equals(c2);z c1.equals(c2);//== true//== true//== trueMIT 6.092 IAP 2006 9Something Strangerz Object o1 = new CheckingAccount(100);Object o2 = new CheckingAccount(100);z o1.equals(o1); z o2.equals(o2);z o1.equals(o2);Compile-time Type Run-time Type//== true//== true//== trueMIT 6.092 IAP 2006 10Compile-time V.S. Run-timez Compile-time typez Type known ahead of time, at time of writing the code—at compile timez During the lifetime of the program, the compile time type never changes for a given instancez Run-time typez The compiler doesn’t have a way of knowing what the runtime type of an object isMIT 6.092 IAP 2006 11Method Dispatchz Even though our objects were of compile-time type Object, the equals method of the CheckingAccount class was calledz This occurs because Java chooses to call the method of the instance’s run-time type and not the compile-time typez Let’s look at another example of method dispatchMIT 6.092 IAP 2006 12Example: BankAccountpublic abstract class BankAccount {…public void withdraw (int amount) {…}…}z Now, CheckingAccount and SavingsAccount are overriding the withdraw methodMIT 6.092 IAP 2006 13Example: BankAccountBankAccount b1 = new CheckingAccount(10);BankAccount b2 = new SavingsAccount(10);b1.withdraw(5);//calls CheckingAccount.withdraw(int)b2.withdraw(5);//calls SavingsAccount.withdraw(int)MIT 6.092 IAP 2006 14Example: Function Arguments…public static boolean deleteAccount (BankAccount accnt) {…}…z Can pass a CheckingAccount or SavingsAccount, the compiler cannot knowcachedBrain.addTextChannel("Messages");cachedBrain.addTextChannel("Messages");MIT 6.092 IAP 2006 15Advantages Of Using General Typesz Can change the underlying implementation laterz Don’t have to change code because only use methods from more general typez Example: Collection v.s. LinkedList and ArrayListMad LibsMadLibTemplateMadLibTemplateMadLibTemplateMadLibTemplateMadLibTemplateis ais


View Full Document

MIT 6 170 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?