Cmsc131 review

Cmsc131 review - if(e instant of bird){ - will be false...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Cmsc131 review F1.equals(f2) will tell if components are same f1 == f2 – will tell if distinct on heap Animal int size void eat() void walk() Bird int wingspan void fly() Eagle int weight void eat() Animal a = new Bird() – valid a.eat() – animal’s eat animal b = new Eagle() – valid b.eat() – eagle’s eat bird c = new Animal()- invalid eagle d = new bird() – invalid bird e = new Eagle() –valid e.fly() – bird’s fly e.eat() – eagle’s eat Animal e = new animal() Eagle f = new eagle() animal h = new eagle() e.size = 10 – valid f.size = 10 – valid h.size = 10 e.weight = 17 – invalid f.weight = 23 - valid h.weight = 44 - invalid if(h instant of eagle){ ((eagle)h).weight =44; } e.fly(); - invalid
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if(e instant of bird){ - will be false ((Bird)e).fly()- downcasting } f.fly() – valid (f is an eagle reference) h.fly() – invalid (animal reference) if (h instance of bird){ - true ((Bird)h).fly(); } e.eat() – valid (calls animal eat) f.eat() – valid (calls eagle eat) Animal e = new animal() Eagle f = new eagle() Animal h = new eagle() (Eagle)h - (Animal)f – upcasting runs (eagle)e – Throws exception (eagle ref to ani) (string)f – wont run Animal g = new Eagle(); g.eat() which version of the eat will run? eagle g.fly(); - invalid ((Bird)g).fly() - calls bird g.fly(5); - invalid ((Eagle)g).fly(5) - calls eagle...
View Full Document

This note was uploaded on 02/05/2009 for the course CMSC 131 taught by Professor Plane during the Fall '08 term at Maryland.

Page1 / 2

Cmsc131 review - if(e instant of bird){ - will be false...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online