DOC PREVIEW
WUSTL CSE 131 - sp14_10

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Classes InheritanceSlide 11Abstract Methods and ClassesAbstract Methods and ClassesPolymorphismDynamic Dispatch and PolymorphismType Consistency in Class HierarchiesChecking Object TypesBeware of instanceofPolymorphism VersionCSE 131 Computer Science 1Module 10: Hierarchiesclass inheritance and class hierarchies2345678910Classes InheritanceClasses may form a hierarchy (general  specific) »Capture common features in a general class Java allows such hierarchy: a new class can extend an existing class»the new class is called the subclass »the original is called the superclass»the subclass inherits all data and methodsin its superclassAccess permissions for subclass»private instance variables and methods can only be accessedwithin the class they belong to»protected instance variables andmethods can be accessed bysubclassesShape- lineColor- lineStyleOneDim- startStyle- endStyleLine- p1, p2Curve- p[..]TwoDim- fillColorRect- center- width- heightEllipse- radius1- radius211To extend a class, use the extends keyword» public class Ellipse extends twoDim { ... }Using inherited methods»Superclass has a method foo(){…}»A subclass can use foo() without defining it itselfA subclass can override a method from a superclass»Superclass has foo(){…}; a subclass can re-define and thus override foo(){…};»can access superclass’ version of a method foo as super.foo(..)A subclass constructor may invoke its superclass’ constructor using super(..)12Abstract Methods and ClassesA class can claim an abstract method without defining it»Leave it for its subclass to defineA class with one or more abstract methods is called an abstract class and must be labeled abstractpublic abstract class twoDim extends Shape { ...public abstract double area();}public class Rect extends twoDim { ..double width, height; ...public double area() { return width*height; }}One cannot create objects belonging to an abstract class»but can create objects belonging to “concrete” subclasses»a concrete subclass of an abstract class must define all abstract methods of its superclasses13Abstract Methods and ClassesAn abstract method is a method that is identified in a superclass and defined in its subclassesA class with one or more abstract methods is called an abstract class and must be labeled abstractpublic abstract class Animal { ...public abstract String speak();}public class Cat extends Animal { ..public String speak() { return “Meow”; }}One cannot create objects belonging to an abstract class»but can create objects belonging to “concrete” subclasses»E.g. Animal x = new Cat();»The type at declaration stays forever. For example, the type of x is Animal, not Cat.14PolymorphismA class hierarchy may contain multiple “versions” of the same methodJava determines at runtime which version of a method to runthis is called dynamic method dispatching15Dynamic Dispatch and PolymorphismThis allows us to process a collection of related but distinct items in a consistent wayExample:public void speaking(Animal x ) { x.speak(); }let each animal speak its own language, using the appropriate speak() method for each animal»this is an example of polymorphism16Type Consistency in Class HierarchiesWe can use an object of a subclass, anywhere a superclass object is called for» Animal x = new Bear();Sometimes, we want to use a superclass object where a subclass is required»this requires a cast: Animal x = new Bear(); // now I want to create a same Bear Bear y = (Bear) x;»typically, we “know” the object has the appropriate typeWe can never cast “across the hierarchy”Superclass method can’t use a subclass’s own methods»x.hibernate() is NOT OK»y.hibernate() is OK»((Bear)x).hibernate() is OK »Read http://stackoverflow.com/questions/10021603/calling-a-subclass-method-from-superclass17Checking Object TypesThe instanceof operator can be used to distinguish objects of different types »if (x instanceof Cat) { /* process cats */ } else { /* process other animals */ }lets us apply case-specific processing for different types»note: instanceof is true for subtypes of the specified class•that is, if x is a Cat, (x instanceof Animal) is trueThe getClass() method can be used to check if two objects have exactly the same type»so, (x.getClass() == y.getClass()) is true if x and y are both Cats, but not if x is a Cat and y is an Animal18Beware of instanceofUse it only as a last resort»It defeats the purpose of polymorphism"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself. -- Scott Meyerspublic final class BadInstanceOf { public void move(Animal aAnimal){ if (aAnimal instanceof Fish){ Fish fish = (Fish)aAnimal; fish.swim(); } else if (aAnimal instanceof Spider){ Spider spider = (Spider)aAnimal; spider.crawl(); }} public class Fish extends Animal { public void swim(){} } public class Spider extends Animal { public void crawl(){} } }19Polymorphism Versionpublic abstract class Animal{ public abstract void move(); } public class Fish extends Animal{ public void move() { swim();} void swim() { ….} } public class Spider extends Animal{ public void move() { crawl();} void crawl() { ….} } …. public class NoInstanceOf { public void move(Animal aAnimal){ aAnimal.move();


View Full Document

WUSTL CSE 131 - sp14_10

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_3

sp14_3

29 pages

sp14_2

sp14_2

43 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

sp14_5

sp14_5

55 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_10
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 sp14_10 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 sp14_10 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?