60 likes | 166 Views
Dive into the intricacies of inheritance and type casting in Java with this revision guide. Explore various scenarios involving class hierarchies like Employee, Manager, and Contractor. We'll analyze code snippets to determine which statements compile successfully and which do not, alongside understanding polymorphic assignments. This guide aims to clarify common misconceptions in object-oriented programming and strengthen your grasp of inheritance and casting concepts, ensuring you’re well-prepared for practical coding challenges.
E N D
Study the following class diagram and code. Which one of the following is correct? Employee Employee e; Manager m = new Manager(); e = m; • This will compile because variable m is of type Manager which is a subtype of Employee, hence polymorphic assignment • This will not compile because m must be the same type as e • This will compile but throw an exception at runtime • I don’t know Manager Contractor
Study the following class diagram and code. Which one of the following is correct? Employee Manager m; Contractor c = new Contractor(); m = c; • This will compile since both m and c are related through the common Employeesupertype • This will compile but fail at runtime • This will not compile since c is neither the same type as m nor a subtype of m, so polymorphic assignment is disallowed • I don’t know Manager Contractor
Study the following class diagram and code. Which one of the following is correct? Employee Employee e = new Employee(); Manager m; m = e; • This will compile since m is a subtype of Employee • This will not compile since it breaks the polymorphic assignment rule • This will compile but fail at runtime • I don’t know Manager Contractor
Study the following class diagram and code. Which one of the following is correct? Employee Employee e = new Employee(); Manager m; m = (Manager)e; • This will not compile since it breaks the polymorphic assignment rule • This will compile but fail at runtime • This will compile and run correctly • I don’t know Manager Contractor
Study the following class diagram and code. Which one of the following is correct? Employee • This will not compile since it breaks the polymorphic assignment rule • This will compile but fail at runtime • This will compile and run correctly • I don’t know Employee e = new Manager(); Manager m; m = (Manager)e; Manager Contractor