Download presentation
Presentation is loading. Please wait.
Published byHilary Murphy Modified over 9 years ago
1
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 106 - Spring 2008 Today’s Topics Comments and/or Questions? protected vs. public vs. private
3
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.
4
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.
5
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...
6
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 Cats do not. Data to be stored in Pet is: – Name – 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.
7
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.
8
abstract methods The reason we create an abstract method (with an empty method body) in the superclass is so that this guarantees that all subclasses must provide this method if they are to be instantiable. Make the method abstract if the superclass doesn't need to be instantiable and if there is no obvious default behavior for the method in the superclass --- this passes the requirement of the behavior of the abstract method to any subclass that wants to be instantiable. Notice: –a subclass of Pet had to include code for the speak() method –we couldn't instantiate an object of type Pet because it is abstract
9
Polymorphism Polymorphism --- having many forms A polymorphic reference variable is one that can refer to different types of objects at different points in time An example from a text I used in the past (Lewis and Loftus): obj.doIt(); If obj is polymorphic --- meaning it can take on the value of different types of objects throughout the running of the program then it might be calling different versions of the doIt() method. What does that mean --- different versions of the doIt() method?
10
Polymorphism It means doIt() may be a method that is in multiple classes and the actual method that gets executed depends on the type of the object. You can imagine that line of code: obj.doIt(); may be in a loop. Then, if the obj reference changes and actually refers to different types of objects, the different versions of doIt() are called. This deals with the concept of binding.
11
Binding Binding of a variable to a type usually occurs at compile-time. This means that when Java compiles the source code, it can figure out the exact type (class) associated with each variable name in the code at the time the code is compiled. In the case of polymorphism, this binding of a variable to a type can only be done at runtime. Why? This is called late binding or dynamic binding. Java needs to determine the ACTUAL type of the object being referred to by the reference. Pet p; // some code here to assign an object to p p.speak();
12
Polymorphism via Inheriance It can go futher. If Pet inherits from LivingThing, then this is allowed: LivingThing lt; Pet a_pet; Dog d = new Dog(“Max”); a_pet = d; lt = d; Here, the Dog reference is being stored in a reference (a_pet) of its immediate superclass (Pet) and also stored in a reference (lt) of a higher superclass (LivingThing.)
13
Polymorphism via Inheriance Below, some_pet is polymorphic because it can refer to a Dog object or a Cat object (or a Pet object if Pet is not abstract.) Pet some_pet = new Dog(“Max”); System.out.println(some_pet.speak()); // Because there are subclasses of Pet (namely Dog // and Cat) the some_pet reference can refer to different // types of objects -> Java must know which speak() // method to execute (the one in Dog, Cat or Pet)
14
Polymorphism via Inheriance Pet some_pet = new Dog(“Max”); some_pet = new Cat(“Kitty”); System.out.println(some_pet.speak()); That's where the dynamic binding of variable to actual type comes into play. Above, some_pet was a reference to a Dog then it became a reference to a Cat, so the speak() method is the Cat's speak method that it executes.
15
Polymorphism via Inheriance Let's add some code that stores an array of Pet references, but each object actually stored in the Pet references may be a Dog, Cat or TalkingDog object.
16
Overriding methods Polymorphism is possible via inheritance because of overriding methods. A subclass can override a superclass's method by providing a definition for a method that exists in the superclass with the same name and number and type of parameters. A class containing an abstract method is not instantiable (can't create objects of this class) instead it is used as a superclass. –If a subclass wants to be instantiable then the subclass that derives from the abstract superclass is required to implement (provide code for) any abstract methods in the superclass.
17
Overriding methods speak() was an abstract method in the parent class Pet. All subclasses of Pet were required to implement that method if the subclasses were to be instantiable. If speak() did not live in Pet, then we would still be allowed to have speak() methods in all the subclasses but we would lose a couple of benefits: –1) nothing requires the signatures of the speak() methods in each subclass to be the same. –2) we won't be able to call the speak() method using a reference to a Pet.
18
Polymorphism Being able to call the speak() method on a reference to a Pet --- that's the key concept here. Only during runtime, during the time that the actual method call occurs does Java know what kind of object is referenced by a Pet reference. A Pet reference could refer to a Dog, Cat or TalkingDog object (because these classes all derive from Pet directly or indirectly.) Java then calls the appropriate speak() method based on which kind of object is actually calling speak() (i.e. a Dog, Cat or TalkingDog).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.