Publications: 75 | Followers: 0

CS18000_ Problem Solving and Object-Oriented Programming

Publish on Category: Birds 0

CS18000: Problem Solving and Object-Oriented Programming
Polymorphism
Abstract ClassesPolymorphismDynamic Binding
Polymorphism
“Many forms”Animals can take on many forms … yet exhibit similar behaviors.Java allows a superclass variable to contain a reference to a subclass objectThe compiler chooses the subclass implementation of any overridden methodsaccount.withdraw(amount);could be savings acct, checking acct, money market acct, ….
3
Code Reuse
Suppose you’re modeling animalsDogCatFishHorse…Lots of redundancy, so you create a superclassAnimalAll other subclasses extend AnimalQ: But what does “new Animal()” mean?A: Nothing--don’t want to create a “generic” animal
4
Abstract Classes
The Java solution for Animal: an abstract classDeclaring a class abstract means that it cannot be instantiatedSome methods may be unimplemented (just like an interface)But an abstract class may also include some implemented methods for default behavior
5
Animal
public abstract class Animal {abstract void speak();public static void main(String[]args) {Animal[] animals = new Animal[2];animals[0] = new Cat();animals[1] = new Dog();for (inti= 0;i<animals.length;i++)animals[i].speak();}}
6
Cat and Dog
public class Cat extends Animal {void speak() {System.out.printf("Meow\n");}}public class Dog extends Animal {void speak() {System.out.printf(“Bark\n");}}
7
Dynamic Binding
Methods are selected at runtime based on the class of the object referenced, not the class of the variable that holds the object referenceExampleAnimal[] animals = new Animal[100];animals[i].speak();Ifanimals[i]is aDog,callsthe speak() method in Dog, even though the variable is of type Animal
8
Why polymorphism?
Allows generic treatment of objectsAn array of AnimalsSome are DogsSome are CatsSome are new animal classes defined after the superclass code is writtenProgrammer must be disciplined: the overridden methods should implement “consistent” or “expected” behaviorExample: In Java, all GUI widgets are a subclass of Component; allows uniform treatment by GUI code
9
Reminder: Subclass Object
Contains its fields as well as all the fields defined in itssuperclasses…
10
name

name

Dog object
Fields defined in Animal
Fields defined in Dog
Fields defined in Object
Revised: Dog
public class Dog extends Animal {private Stringname;public Dog(String name) {super(name);this.name =super.getName()+ " Barker";}public StringgetName() {return name;}void speak() {System.out.printf("Bark\n");}}
11
Revised: Animal
public abstract class Animal {private Stringname;public Animal(String name) {this.name= name;}publicStringgetName() {return name;}abstract void speak();public static void main(String[]args) {Animal[] animals = new Animal[2];animals[0] = new Cat("Garfield");animals[1] = new Dog("Snoopy");for (inti= 0;i<animals.length;i++)animals[i].speak();Dog d = new Dog("Marmaduke");System.out.println(d.getName());Animal a = d;System.out.println(a.getName());}}
12
Abstract Methods
Methods may be declared abstractProvide only the header (no body)Class must then be declared abstractMethods in an interface are implicitly declared abstractWhensubclassingan abstract classGenerally provide method bodies for abstract methodsIf abstract methods remain, then subclass is still abstract and must be declared so
13
Example: Abstract Methods
abstract public classAbstractParent{abstractvoid doOne();abstractvoiddoTwo();}abstract classAbstractChildextendsAbstractParent{voiddoOne() {System.out.println("inAbstractChild");}}classConcreteGrandChildextendsAbstractChild{voiddoTwo() {System.out.println("inConcreteGrandChild");}}public static void main(String[]args) {ConcreteGrandChildcc =newConcreteGrandChild();cc.doOne();cc.doTwo();}
14

0

Embed

Share

Upload

Make amazing presentation for free
CS18000_ Problem Solving and Object-Oriented Programming