Part 13: Legacy and Polymorphism.


55 views
Uploaded on:
Category: Fashion / Beauty
Description
A makeSound() technique may be diverse for a ferret and a platypus ... Your platypus class may have a swim() system, yet ferrets can't swim! Acquiring From ...
Transcripts
Slide 1

Part 13: Inheritance and Polymorphism CS180 Fall 2006

Slide 2

Announcements Project 5 reviewing is… sit tight for it… sit tight for it… DONE! Evaluations will be discharged by ahead of schedule one week from now Gradesheets gave back in your labs Hand back exam 2 Mean: 67.8 Median: 71 High: 97 Next week: generics Online section You ought to have the capacity to get to it with the watchword that accompanied your book (on the off chance that you purchased it in the book shop) Or… go here: http://www.cs.purdue.edu/~mrcarlso/cs180 We will give a client/secret key for you

Slide 3

Definitions Inheritance – object situated approach to shape new classes from previous ones Superclass The guardian class If class is last , can\'t acquire from this class Subclass Class which acquires all non-private properties from the superclass Generally a particular example of a superclass (Animal->Ferret) Polymorphism – an article can take numerous structures Not all creatures are ferrets, could be platypus\' also A makeSound() technique might be distinctive for a ferret and a platypus Combining the two, can make subclasses with various properties Your platypus class may have a swim() strategy, however ferrets can\'t swim!

Slide 4

Inheriting From the Superclass Two ways Use the ensured catchphrase rather than the private watchword Variables and strategies which are secured are available by the subclass, however not the world Use open for techniques to acquire, private for strategies to not acquire and all Variables no more available straightforwardly in subclass Use accessors and mutators to adjust them Overwrite open (or secured) techniques as vital If a strategy is proclaimed last , then it can\'t be overwritten in a subclass Can likewise over-burden techniques if craved Constructors Not acquired! Must revise them Use super watchword to call superconstructor from guardian class Can just utilize super as first line of a constructor If no superconstructor is expressly called, then super() (the default superconstructor) is consequently called as the principal line super catchphrase can likewise be utilized to call occurrences of strategies from superclasses

Slide 5

Syntax To augment a class, utilize the develops catchphrase The class will now acquire all non-private attributes of the superclass Do not need to re-compose techniques which are the same Can present new strategies and/or variables open class A { private int x; public A() { set(0); } public A(int x) { set(x); } public void set(int x) { this.x = x; } public int get() { return x; } public void add1() { knock(); } private void knock() { x++; } open class B augments A { public B() { super(); } public B(int x) { super(x); } public void add1() { set(get()+1); }

Slide 6

Inheriting From the Superclass open class A { protected int x; public A() { set(0); } public A(int x) { set(x); } public void set(int x) { this.x = x; } public int get() { return x; } public void add1() { knock(); } private void knock() { x++; } open class B broadens A { public B() { super(); } public B(int x) { super(x); } open class A { private int x; public A() { set(0); } public A(int x) { set(x); } public void set(int x) { this.x = x; } public int get() { return x; } public void add1() { knock(); } private void knock() { x++; } open class B expands A { public B() { super(); } public B(int x) { super(x); } public void add1() { set(get()+1); } The add1() strategy is overwritten here!

Slide 7

Inheriting From the Superclass On the superclass constructor calls, constructors are executed top-down on the chain of importance. open class A { A() { SOP("A"); } open class B augments A { public B() { SOP("B"); } open class C amplifies A { public C() { SOP("C"); } open class D develops B { public D() { SOP("D"); } A = new An(); Output: A B = new B(); Output: AB C = new C(); Output: AC D = new D(); Output: ABD

Slide 8

Object Class Object is the superclass of all superclasses in Java All classes characterized verifiably acquire from Object if not unequivocally stretching out from another class Even if expressly broadening another class, that different class either reaches out from another class or certainly reaches out from Object Some valuable techniques which are regularly overwritten measures up to – for examinations (defaults to reference address comparision) toString – makes an interpretation of your item into a fitting String (defaults to the reference address)

Slide 9

Polymorphism open class Animal { protected int age; public void makeSound() { SOP("Make sound!"); } open class Ferret expands Animal { public Ferret() { super(); } public void eatSweets() { SOP("Yummy!"); } public void makeSound() { SOP("Squeal!"); } open class Platypus augments Animal { public Platypus() { super(); } public void swim() { SOP("Swim!"); } public void makeSound() { SOP("Quack!"); } _____________________________________________ Animal a = new Ferret(); a.swim(); /illicit! Not all creatures know how to swim! a.makeSound();/lawful! All creatures make sounds! a.eatSweets();/unlawful! Just ferrets like confections and treats! Permits a variable to go up against numerous structures A variable of sort Animal may indicate may distinctive sorts of creatures Only techniques checked inside the variable sort are accessible Not all creatures swim, so despite the fact that an Animal item may indicate a platypus, you can\'t call the swim() strategy

Slide 10

Dynamic Binding On the call the makeSound(), what happens? On the off chance that Animal protest a will be a Platypus, then "Quack!" If Animal question a will be a Ferret, then "Screech!" Why not "Make sound!"?  dynamic official! Objects have two sorts Static sort – the kind of the variable name Dynamic sort – the "real sort" of the variable in memory Dynamic restricting Used for strategy query Look up "most particular" example of technique call (turn upward the technique in the dynamic sort in the event that it exists) Most particular case of a technique might be in a guardian class

Slide 11

Dynamic Binding open class A { protected int x; public A() { set(0); } public A(int x) { set(x); } public void set(int x) { this.x = x; } public int get() { return x; } public void knock() { x+=2; } open class B augments A { public B() { super(); } public B(int x) { super(x); } public void knock() { x++; } static sort dynamic sort An a = new B(); a.bump(); SOP("a="+a.get()); Object an is of static sort A, dynamic sort B. On a strategy call, use dynamic sort first!

Slide 12

Determining Class Type to get to a technique in a subclass from an item with a static kind of the superclass, need to sad Downcasting is perilous! Need to realize that depressed is lawful. Use instanceof catchphrase or different strategies x instanceof A – genuine if x is a case of class An or a subclass of A x.getClass().equals(A.class) – genuine if x is an example of class An A.class.isAssignableFrom(x.getClass()) is valid if x is an occasion of class An or a subclass of A

Slide 13

Determining Class Type open class Animal { protected int age; public void makeSound() { SOP("Make sound!"); } open class Ferret develops Animal { public Ferret() { super(); } public void eatSweets() { SOP("Yummy!"); } public void makeSound() { SOP("Squeal!"); } open class Platypus expands Animal { public Platypus() { super(); } public void swim() { SOP("Swim!"); } public void makeSound() { SOP("Quack!"); } Animal a = new Ferret(); if (an instanceof Ferret) ((Ferret)a).eatSweets(); a = new Platypus(); if (a.getClass() == Platypus.class) ((Platypus)a).swim(); if (Platypus.class.isAssignableFrom(a)) ((Platypus)a).swim(); ________________________________ Animal a = new Ferret(); ((Platypus)a).swim(); /aggregates, yet runtime mistake!

Slide 14

Abstract Classes Sometimes a superclass never should be instantiated – you just make occasions of the subclasses Make class theoretical Cannot be instantiated May have a few strategies be conceptual resemble an outline for subclasses, with techniques that should be filled in broad daylight unique class Animal { protected int age; public dynamic void makeSound(); } open class Ferret augments Animal { public Ferret() { super(); } public void eatSweets() { SOP("Yummy!"); } public void makeSound() { SOP("Squeal!"); } open class Platypus broadens Animal { public Platypus() { super(); } public void swim() { SOP("Swim!"); } public void makeSound() { SOP("Quack!"); } }

Slide 15

Interfaces Contains strategy headers Cannot instantiate interface Cannot have any non-last variables A class can actualize numerous Interfaces can reach out from each other Example: Comparable Contains one strategy, compareTo, which takes two protests and thinks about them Returns an adverse number if not as much as Returns 0 if equivalent to Returns a constructive number if more noteworthy than Comparable is an interface and not a conceptual class in light of the fact that not all things similar are connected Numbers and individuals are both tantamount, yet have no sensible relationship between them

Slide 16

Interfaces open interface I { public void doStuff(); } open interface J amplifies I { public void doMoreStuff(); } open interface K { public void doEvenMoreStuff(); } open class An executes J, K { public void doStuff() {… } public void doMoreStuff() {… } public void doEvenMoreStuff {… } J develops I, so interface J really has 2 strategies: doStuff() and doMoreStuff(). An executes J and K, so An absolute necessity have a strategy for every technique in the interfaces it actualizes.

Slide 17

Putting It All Together open interface SoundMaker { public void makeSound(); } open interface EggLayer { public void layEgg(); } open interface Swimmer { public void swim(); } open dynamic class Animal executes SoundMaker { protected int age; public theoretical void makeSound(); } open class Ferret expands Animal { public Ferret() { super(); } public void eatSweets() { SOP("Yummy!"); } public void makeSound() { SOP("Squeal

Recommended
View more...