COMP 274 Week 2 Homework Assignment

Published on January 2018 | Categories: Technology | Downloads: 108 | Comments: 0 | Views: 571
of 1
Download PDF   Embed   Report

Reading Lecture Chapter 8: Objects and Classes Sections 8.3, 8.5, 8.7, 8.8, 8.9, 8.10, 8.11 Chapter 10: Thinking in Objects Sections 10.4, 10.5, 10.7, 10.11 Chapter 11: Inheritance and Polymorphism Sections 11.2, 11.3, 11.4, 11.7, 11.8, 11.9, 11.13, 11.14 Create a Word document containing the answers to all the problems given below. Can you access an instance variable from a static method? Explain why or why not. Can you access a static variable from an instance method? Explain why or why not. Consider the case where you have two classes, ClassA and ClassB, such that ClassA contains a member variable defined as ClassB myBobj. ClassB contains a constructor which takes a String parameter. ClassA provides a constructor which accepts a String parameter which is to be provided to myBobj. What needs to be done to initialize the ClassB member variable with the value from the ClassA constructor? Here are some class definitions with questions that follow. public class BaseClass { // methods of BaseClass } public SomeClass extends BaseClass { // methods of SomeClass } a.SomeClass scobj = new SomeClass( ); b.BaseClass bcobj = scobj; c.SomeClass scobj2 = bcobj; d.bcobj.aMethodOfSomeClass( ); // does not exist in BaseClass A.Is statement b above legal, or is casting required? B.Statement c is trying to assign the SomeClass object referred to by bcobj to a SomeClass reference variable scobj2. What is wrong with this statement and how would you fix it. C.In statement d, a BaseClass reference is trying to access a SomeClass class method that is not part of BaseClass. Is this legal? Explain. Give an example using Java syntax of how some class Y can be setup to inherit from some other class X. What is a significant difference between C++ and Java inheritance? How is the keyword super used? Give two different uses for it and include Java examples. Explain the difference between public, private, and protectedaccess specifiers. Give some guidelines of when each should be used. Explain what package access means and how it is specified. What can you do in Java to prevent a method from being overridden in a derived class? Give example code in your answer. How do you prevent a class from being extended? Give example code in your answer. Explain how dynamic binding works in Java.

Comments

Content

Reading Lecture Chapter 8: Objects and Classes Sections 8.3, 8.5, 8.7, 8.8, 8.9, 8.10, 8.11 Chapter 10: Thinking in Objects Sections 10.4, 10.5, 10.7, 10.11 Chapter 11: Inheritance and Polymorphism Sections 11.2, 11.3, 11.4, 11.7, 11.8, 11.9, 11.13, 11.14 Create a Word document containing the answers to all the problems given below. Can you access an instance variable from a static method? Explain why or why not. Can you access a static variable from an instance method? Explain why or why not. Consider the case where you have two classes, ClassA and ClassB, such that ClassA contains a member variable defined as ClassB myBobj. ClassB contains a constructor which takes a String parameter. ClassA provides a constructor which accepts a String parameter which is to be provided to myBobj. What needs to be done to initialize the ClassB member variable with the value from the ClassA constructor? Here are some class definitions with questions that follow. public class BaseClass { // methods of BaseClass } public SomeClass extends BaseClass { // methods of SomeClass } a.SomeClass scobj = new SomeClass( ); b.BaseClass bcobj = scobj; c.SomeClass scobj2 = bcobj; d.bcobj.aMethodOfSomeClass( ); // does not exist in BaseClass A.Is statement b above legal, or is casting required? B.Statement c is trying to assign the SomeClass object referred to by bcobj to a SomeClass reference variable scobj2. What is wrong with this statement and how would you fix it. C.In statement d, a BaseClass reference is trying to access a SomeClass class method that is not part of BaseClass. Is this legal? Explain. Give an example using Java syntax of how some class Y can be setup to inherit from some other class X. What is a significant difference between C++ and Java inheritance? How is the keyword super used? Give two different uses for it and include Java examples. Explain the difference between public, private, and protectedaccess specifiers. Give some guidelines of when each should be used. Explain what package access means and how it is specified. What can you do in Java to prevent a method from being overridden in a derived class? Give example code in your answer. How do you prevent a class from being extended? Give example code in your answer. Explain how dynamic binding works in Java.

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close