13_Review Lecture

s1popalls2 should be ok since classa is a super type

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: requests a refund because they want more flexibility 18 Are we losing polymorphism? class Stack<T>{! public void pushAll(Stack<T> s){! for(//there is element in me)! s.push(pop());! }! public void popAll(Stack<T> s) {! for(//there is element in s)! push(s.pop());! }! }! //Let us say ClassB is a ! //subclass of ClassA! Stack<ClassA> s1 = new Stack<ClassA>();! ! Stack<ClassB> s2 = new Stack<ClassB>();! ! s2.pushAll(s1); ! //should be OK since ClassB is also a ! //type of ClassA. s1 that handles the ! //super type can handle subtypes! ! s1.popAll(s2); ! //should be OK since ClassA is a super! //type of ClassB. Again, s1 should be ! //able to take subtypes of ClassA.! ! ! It seems that parameters should behave polymorphically 19 We need “wild card” •  In defining a generic type –  “E extends ClassA” represents all subtypes of ClassA •  In defining a container variable that b...
View Full Document

This document was uploaded on 03/11/2014.

Ask a homework question - tutors are online