Midterm(2(Review(Decorator(COMP(401,(Spring(2013(Lecture(17(3/19/2013((How(Now(Brown(Cow((Professor?)(• Revised(assignment(schedule:(– A5:(Wed.(3/27(– A6:(Fri.(4/5(– A7:(Wed.(4/17(– A8:(Fri.(4/26(• Midterm(2(sTll(this(Friday(• Today(– Midterm(2(Review,(start(of(Decorator(• Thursday(– Decorator(finished,(start(of(Observer/Observable(Inheritance(• Subinterfacing(– Extending(an(exisTng(interface(• Subclassing(– Extending(an(exisTng(class(Extending(Interfaces(• Adds(methods(to(contract.(– Original:((• parent(interface,(super(interface(– New:(• subinterface,(child(interface,(extended(interface(• Created(by(using(the(“extends”(keyword.(public(interface(CompressedMedia(extends(Media({((((int(getCompressedSize();((((int(getUncompressedSize();((((Media(uncompress();(}(Extension(Creates(Hierarchy(• IsdA(relaTonship(is(transiTve(up(the(hierarchy.(Media(Compressed(Media(extends(public(class(Song(implements(CompressedMedia({(...(Methods(for(both(must(be(provided.(Song(s(=(new(Song();((CompressedMedia(cm(=((CompressedMedia)(s;((Media(m(=((Media)(s;((Song(s2(=((Song)(m;(OK#because#s#“is#a”#Compressed#Media#OK#because#s#is#a#Media#by#virtue#of#extension.#Cas;ng#from#interface#back#to#specific#object#type#is##allowed,#but#at#run;me,#if#the#object’s#type#does#not#actually#match,#a#run;me#excep;on#will#be#thrown.#Interface(Extension(vs.((Interface(ComposiTon(public(interface(Compressed({((((int(getCompressedSize();((((int(getUncompressedSize();((((Media(uncompress();(}(public(interface(Media({(((((int(getLengthInSeconds();(((((double(getLengthInMinutes();(((((int(getRaTng();(((((void(setRaTng(int(new_raTng);(((((String(getName();(}(public(class(Song(implements(Compressed,(Media({(...(Instead#of#extending#Media,#Compressed#is#a#separate#interface#and#Song#implements#both.#Song(s(=(new(Song();((Media(m(=((Media)(s;((Compressed(c(=((Compressed)(s;(Song#“is#a”#Media#AND#Song#“is#a”#Compressed.#Subinterface(vs.(Subclass(• Extending(interface(only(added(behavior(to(contract.(– Since(interfaces(don’t(specify((and(don’t(care)(how(contract(is(fulfilled.(• Extending(class(creates(a(new(class(that(shares(internal(implementaTon(details(of(its(super(class.(COMP(401(::(Spring(2012( 7(IsdA(For(Subclasses(COMP(401(::(Spring(2012( 8(class(A(implements(InterA({(...(}(((class(B(extends(A(implements(InterB({(...(}((((class(C(extends(B(implements(InterC({(...(}(Objects(of(type(A,(implement(interface(InterA.((A(“is(a”(Inter(A(Objects(of(type(B,(implement(interface(InterB(and(InterA.((B(“is(a”(A((B(“is(a”(InterA((B(“is(a”(InterB(((Objects(of(type(C,(implement(interface(InterC,(InterB,(and(InterA.((C(“is(a”(A((C(“is(a”(B((C(“is(a”(InterA((C(“is(a”(InterB((C(“is(a”(InterC(((Instance(Fields(• Subclass(has(direct(access(to(public(and(protected(instance(fields.(– Public: (Everyone(has(access(• Generally(not(a(good(idea.(• Breaks(encapsulaTon.(– Private:(Only(class(has(access(• Generally(recommended(as(default.(• Subclasses,(however,(also(shut(out.(– Protected:(Class(and(subclasses(have(access.(• Like(private((i.e.,(appropriate(use(of(encapsulaTon)(but(allows(for(subclassing((even(if(outside(of(package)(• lec9.v4(COMP(401(::(Spring(2012( 9(Access(Modifier(Chart(Class% Package% Subclass% World%public% YES( YES( YES( YES(protected% YES( YES( YES( NO(no#modifier# YES( YES( NO( NO(private% YES( NO( NO( NO(Subclass(Method(Polymorphism(• Subclass(can(overload(methods(in(superclass.(– Remember,(overloading(is(providing(a(different(version(of(an(exisTng(method.(• An(example(of(polymorphism(• Method(signature(is(different(in(some(way.(– lec10.v1(Overriding(Methods(• A(subclass(can(“override”(a(super(class(method(by(providing(its(own(definiTon.(– Method(signature(must(be(the(same.(– Original(method(is(visible(from(subclass(• i.e.,(public,(protected,(or(packagedlevel(access((• lec10.v2(Class(Polymorphism(• Previously(introduced(the(idea(of(“isda”(relaTonships(– Between(a(class(and(interfaces(implemented.(– Between(a(class(and(its(superclass(hierarchy.(• This(is(also(an(example(of(polymorphism(– Covariance(• TreaTng(an(instance(of(a(subclass(as(a(reference(typed(as(the(parent(class.((• This(can(be(typed(checked(at(compile(type.(– Contravariance(• TreaTng(a(reference(typed(as(the(parent(class(as(an(instance(of(a(subclass.(• Contravariance(can(not(be(type(checked(in(advance(at(compile(Tme.(• Fails(if(the(object(is(actually(“invariant”(with(respect(to(the(subclass.(• lec10.v4,(lec10.v4main(– Also(demonstrates(protected(base(class(constructor(A(Covariant(Conundrum(• Problem:(– What(should(happen(when(an(overriden(method(is(called(on(a(covariant(reference?(class A {! public int m() {return 0;}!}!!class B extends A {! public int m() {return 1;}!}!!class C extends B { ! public int m() {return 2;}!}!C c_obj = new C();!B b_obj = (B) c_obj;!A a_obj = (A) c_obj;!!System.out.println(c_obj.m());!System.out.println(b_obj.m());!System.out.println(a_obj.m());!What(should(these(lines(print?(Virtual(Methods(• Different(OOP(languages(choose(to(solve(this(problem(in(different(ways.(– C++,(C#(• Default(is(nondvirtual(soluTon.(• Programmer(can(force(virtual(soluTon(by(marking(a(method(with(a(special(“virtual”(keyword(– Java(• Methods(are(always(virtual.(• No(special(keyword(needed.(• lec10.v5(Abstract(Classes(and(Methods(• Parent(class(has(no(meaningful(implementaTon(of(a(method.(– But(part(of(interface(of(parent(class(– Expect(subclass(to(provide(it.(– In(these(situaTons,(we(never(expect((or(want)(the(parent(class(to(be(instanTated(directly.(• We(always(make(new(objects(using(a(subclass.(((• Syntax(– Use(“abstract”(modifier(when(declaring(parent(class(– Declare(any(methods(that(must(be(provided(by(subclass(in(parent(• Add(“abstract”(modifier(to(method(signature.(• Follow(signature(with(semicolon(instead(of(method(definiTon(ComposiTon(and(AggregaTon(• Two(design(techniques(for(creaTng(an(object(that(encapsulates(other(objects.(– Any(specific(situaTon(is(not(necessarily(strictly(one(or(the(other.(• In(a(nutshull…(– ComposiTon(•
View Full Document