CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
Published byModified over 5 years ago
Presentation on theme: "CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann."— Presentation transcript:
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS 106 - Fall 2007 Today’s Topics Comments and/or Questions? overriding methods
Overriding methods public String toString() What do you think about the toString() method? Can that be overridden by Account? Can it be overridden by a subclass of Account, like SavingsAccount?
equals method In class Object, the header for the equals method is this: public boolean equals(Object obj) If we want to override this method, we must make sure our equals method has the SAME signature. Does anyone recall what a signature contains?
Protected vs. Private vs. Public subclasses have access to public and protected members of their superclass a class has access to all of its own members (whether they are private, protected or public) objects of a class have access only to the public members of the class (and the public members of the parent class(es)). Classes within the same package though are allowed to always access protected members --- but I don't recommend this. What does this mean for our Account program? What about inside the CheckingAccount class, what members of Account can we refer to? What about an object of type CheckingAccount --- does it have access to any of the members in Account?
Overloaded methods Overloaded methods are those that have the same name but different numbers or types of parameters. It has nothing to do with the super / subclass (parent / child) relationships that we've been talking about. Does anyone remember when we used Overloaded methods?
Account Program Let's continue to implement in Account, CheckingAccount, SavingsAccount – the equals(Object o) method
Abstract classes – Can never be instantiated – Can contain both abstract methods and actual (non-abstract) methods – Can contain instance variables as well as constants If a class contains any abstract methods then it MUST BE an abstract class But an abstract class is not required to have abstract methods Abstract classes are different from interfaces which we will see next.
More on overriding methods Suppose we have a class Pet, Dog, and Cat. We provide a speak() method in Pet but with no body. Then, any class that inherits from Pet, must implement this method. Further, let's assume we have a class TalkingDog which doesn't bark when he speaks, instead he speaks English. To implement this stuff...
Class Pet, Dog and Cat Let's suppose all Pets have names, breeds and make sounds, and know how to sleep. Let's also suppose that different Dogs have different skills, but different Cats do not. Data to be stored in Pet is: – Name – Type (cat, dog, etc.) – Breed Additional data to be stored in Dog is: – Skill No additional data is stored in Cat. Let's set up the classes with these data and the relationships among the classes.
More on overriding methods So we have classes Pet (abstract), Dog, and Cat. We can provide a speak() method in Pet with no body (and we make it abstract). Then, any class that inherits from Pet, must implement this method. We can provide an actual method sleeps() in Pet that is not abstract. And the class TalkingDog which doesn't bark when he speaks, instead he speaks English. So, Dog and Cat inherit from Pet. TalkingDog inherits from Dog. Let's implement this stuff (instantiate objects of Dog, Cat, and TalkingDog) Note: because Pet contains an abstract method, the class is not instantiable --- the class itself must be declared as abstract.